【发布时间】:2016-07-16 17:50:39
【问题描述】:
我想让 python 问 10 个问题,并且用户必须输入他们的答案,这很有效。但是,我也希望 python 通过使用下面的代码来说明这是否正确,但这不起作用,只能转到下一个问题。谁能告诉我为什么?或者我需要改变什么?另外,如何使用我拥有的变量和 while 循环专门提出 10 个问题?
import time
import random
question = 0
score = 0
name = input("What is your full name?")
print ("Hello " + name, "welcome to The Arithmetic Quiz")
time.sleep(2)
operand1 = list(range(2, 12))
operators = ["+"]
operand2 = list(range(2, 12))
while question < 10:
user_answer=int(input(str(random.choice(operand1)) + random.choice(operators) + str(random.choice(operand2))))
if operators=='+':
expected_answer==operand1 + operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
【问题讨论】:
-
我假设基于
input的使用方式,这是 Python 3?
标签: python loops variables while-loop sum