如果给出合适的答案,如何重复输入?

时间:2016-01-09 16:07:56

标签: python

我已经完成了一个代码,所以它要求用户回答算术问题并告诉他们答案是否正确等等......我开始做一些测试并意识到用户输入的是什么号码给了我一个错误。 我的代码:

import random

    name=input("Welcome to this Arithmetic quiz,please enter your name:")

    score = 0

    for i in range(10):

          number1=random.randint(20,50)

          number2=random.randint(1,20)

          oper=random.choice('+-*')

          correct_answer = eval(str(number1)+oper+str(number2))

          answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == correct_answer)

          if answer:
                  print('Correct!')

                  score += 1

          else:
                  print('Incorrect!')

    print(name,"You got",score,"out of 10")

    if score>1 and score<=3 :
                  print('Practice More!')

    elif score>4 and score<=7 :
                  print('You did well!')

    elif score>7 and score<=9 :
                  print('Excellent!')

    elif score==10 :
                  print('You are a Genius!')

    else:
                  print('Have you tried your best?')

我想知道在用户输入数字之前如何重复第9行? 这不是一个重复的BECUASE我希望用户特别输入一个数字。如果他/她说它会告诉他们错误或正确并继续下一个问题。

1 个答案:

答案 0 :(得分:0)

我建议将for循环放在while循环中,直到为true,然后退出循环。这回答了你的问题吗?

相关问题