0

I'm making a worded numbers to numbers test for an assessment in school and need to generate a random worded number. I can do that but I need an answer to that random number in order to check if its right or not. I use four lists for the worded questions such as thousands, hundreds, tens and units. Then I print out the worded number using random selections from each list. I have another list with actual numbers in and need to know how to match what the worded number is with the actual number. So if it generates one thousand how do I match that to 1. Or nine hundred how do I match it to 9. Is there a faster way at all? I can provide my code if necessary but its a bit long. I'm pretty much a beginner as well.

import random wordt=['one thousand', 'two thousand', 'three thousand', 'four thousand', 'five thousand', 'six thousand', 'seven thousand', 'eight thousand', 'nine thousand'] wordh=['one hundered', 'two hundered', 'three hundered', 'four hundered', 'five hundered', 'six hundered', 'seven hundered', 'eight hundered', 'nine hundered'] wordte=['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] wordu=['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] print(wordt[random.randint(0,8)], end=' ') print(wordh[random.randint(0,8)], end=' ') print('and', end=' ') print(wordte[random.randint(0,6)], end=' ') print(wordu[random.randint(0,8)])`` number=['1', '2', '3', '4', '5', '6', '7', '8', '9'] 
9
  • 2
    Yes, provide your code Jack Commented Aug 28, 2017 at 21:38
  • 1
    "I can provide my code if necessary but its a bit long." That's what minimal reproducible examples are for. Commented Aug 28, 2017 at 21:38
  • import random wordt=['one thousand', 'two thousand', 'three thousand', 'four thousand', 'five thousand', 'six thousand', 'seven thousand', 'eight thousand', 'nine thousand'] wordh=['one hundered', 'two hundered', 'three hundered', 'four hundered', 'five hundered', 'six hundered', 'seven hundered', 'eight hundered', 'nine hundered'] wordte=['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] wordu=['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] Commented Aug 28, 2017 at 21:44
  • print(wordt[random.randint(0,8)], end=' ') print(wordh[random.randint(0,8)], end=' ') print('and', end=' ') print(wordte[random.randint(0,6)], end=' ') print(wordu[random.randint(0,8)]) number=['1', '2', '3', '4', '5', '6', '7', '8', '9'] Commented Aug 28, 2017 at 21:44
  • @Jack please put that in the original post. Code doesn't format well in comments. Commented Aug 28, 2017 at 21:44

1 Answer 1

1
 import random def AllSame (t): """ Check if string is composed with only one, repeating character. For instance: >>> AllSame("aaaaaaa") True """ if not t: return False l = t[0] c = 1 for x in t[1:]: c += x==l return c==len(t) # English numbers: digits = { "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "0": "zero", "10": "ten", "11": "eleven", "12": "twelve", "13": "thirteen", "14": "fourteen", "15": "fifteen", "16": "sixteen", "17": "seventeen", "18": "eighteen", "19": "nineteen", "20": "twenty", "30": "thirty", "40": "forty", "50": "fifty", "60": "sixty", "70": "seventy", "80": "eighty", "90": "ninety", "100": "one hundred", "200": "two hundred", "300": "three hundred", "400": "four hundred", "500": "five hundred", "600": "six hundred", "700": "seven hundred", "800": "eight hundred", "900": "nine hundred", "00": "", "1000": "one thousand", "2000": "two thousand", "3000": "three thousand", "4000": "four thousand", "5000": "five thousand", "6000": "six thousand", "7000": "seven thousand", "8000": "eight thousand", "9000": "nine thousand", "000": "thousand", "1000000": "million", "000000": "million", "1000000000": "billion", # For declanative languages. Where thousand changes form. # E.g. In Croatian: 1000=Tisucu 21000=Dvadeset i [jedna tisuca] "001": "one thousand", "002": "two thousand", "003": "three thousand", "004": "four thousand", "005": "five thousand", "006": "six thousand", "007": "seven thousand", "008": "eight thousand", "009": "nine thousand"} def NumberToWord (num, digits=digits, lj=" ", kz=0): """ Explodes a number to word(s). digits is a dictionary containing all major number-word mappings. See the example. lj is the word inserted between tens and ones. kz is internal for recursive control, do not use it manually. """ num = num.strip() if kz==1: t = "" for x in num: if x=="0" and len(t)==0: continue t += x num = t if not num: return "" if kz==-1: e = "" for x in num: e += digits[x]+" " return e[:-1] if AllSame(num) and num[0]=="0": return (len(num)*(digits[num[0]]+" "))[:-1] if num.startswith("0"): return NumberToWord(num, digits, lj, -1) if digits.has_key(num): return digits[num] l = len(num) if l==2: return digits[num[0]+"0"]+lj+digits[num[1]] if l==3 or l==4: return NumberToWord(num[0]+((l-1)*"0"), digits, lj, 1)+" "+NumberToWord(num[1:], digits, lj, 1) if l==5: d1 = num[0]; d2 = num[1] if d1=="1": return (NumberToWord(num[:2], digits, lj, 1)+" "+digits["000"]+" "+NumberToWord(num[2:], digits, lj, 1)).strip() return (digits[d1+"0"]+" "+digits["00"+d2]+" "+NumberToWord(num[2:], digits, lj, 1)).strip() if l==6: d1 = num[0]; d2 = num[1]; d3 = num[2] if d2=="1": m = digits[d2+d3]+" "+digits["000"] else: m = digits[d2+"0"]+" "+digits["00"+d3] return (digits[d1+"00"]+" "+m+" "+NumberToWord(num[3:], digits, lj, 1)).strip() return NumberToWord(num, digits, lj, -1) print NumberToWord(str(random.randint(0, 999999))) 
Sign up to request clarification or add additional context in comments.

5 Comments

when I run it, it says NumberToWord at the very bottom is invalid syntax
@Jack : You are probably on Python 3.x, so print is not a statement but a function. You need parenthesis there: print(NumberToWord(...))
line 50, in NumberToWord if digits.has_key(num): return digits[num] AttributeError: 'dict' object has no attribute 'has_key'
In Python 3.x dict.has_key() is removed. Change the line to: if num in digits
NB: Code works in Python 2.x without problems. Tested as this excerpt and it is a part of one big project constantly in use. Any difficulties you encounter with this code will be caused by Python's 3 non backward compatibility.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.