jeudi 29 juin 2017

Python: How to check digits in a string against an array

I'm making something that creates 10 random numbers and outputs them as words. Here is my code so far:

import random as ran
nums = []
numarray = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,'00','000','&']
wordarray = ["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety","hundred","thousand","and"]
for each in range(0,len(numarray)):
    print(numarray[each],"=",wordarray[each])
for each in range(0,10):
    num = ran.randint(0,9999)
    if num < 10:
        num = '000'+str(num)
    elif num < 100:
        num = '00'+str(num)
    elif num < 1000:
        num = '0'+str(num)
    else:
        num = str(num)
    nums.append(num)
print(nums)

I now need to check each digit of the ten random numbers against the arrays "numarray" and "wordarray" so that I can convert it into words. I was wondering would I do this. Could anyone help?

Happy Coding, A Doctor Who




Aucun commentaire:

Enregistrer un commentaire