如何将-for循环-的答案添加到列表中?

时间:2019-04-06 00:49:33

标签: python

'''试图将对此for循环的答案添加到列表中。 '''

import random
name = []

print ("Welcome to the multiplication Game")
cont = 0
name = input(str"Please enter your name")
base = input (str("Enter the number you want to practice on"))
por = [x for x in range(1,10)]
random.shuffle(por) #the numbers in the list will be sorted in a random order
for i in por:
   x= int(input("{} x {} = ".format(base, i)))
   if (x) == int(base*i):
        print ("Correcto!")
        bandera= False
        correcto+=1
    if (x) != int(base*i): 
        print ("Incorrecto!")
        print("La respuesta correcta es ", int(base*i))
        errores +=1

'''我现在决定为此采取另一条路径,并且没有将用户输入添加到列表'''

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

import random
number = input("Enter your number : ")

numbersAlreadyUsed = []

while len(numbersAlreadyUsed) < 11:
    # get a random number within this range
    randNumber = random.randrange(1,11)
    if(randNumber not in numbersAlreadyUsed):
        print("{0} x {1} = {2}" .format(number,randNumber, randNumber * int(number)))
        numbersAlreadyUsed.append(randNumber)
else:
    print("we are done!")

请注意,该代码可以大量重构,并不美观!

谢谢