【发布时间】:2016-07-10 14:31:15
【问题描述】:
我的 while 循环中有三个 if 语句,它们应该在问题变量等于 10 之前提出问题。
但是我认为因为我有三个 if 语句,所以它只问三个问题。我尝试使用 break - 它结束了我的 while 循环
这是我的代码:
while question<10:
user_answer=input(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers1)))
if operators == '+':
expected_answer = numbers + numbers1
if user_answer==expected_answer:
print ('This is correct!')
print ("Your score so far is",score,"out of 10")
question=question+1
time.sleep(2)
print ('This is incorrect!')
question=question+1
time.sleep(2)
if operators == '-':
expected_answer = numbers - numbers1
if user_answer==expected_answer:
print ('This is correct!')
print ("Your score so far is",score,"out of 10")
question=question+1
time.sleep(2)
print ('This is incorrect!')
question=question+1
time.sleep(2)
if operators == '*':
expected_answer = numbers * numbers1
if user_answer==expected_answer:
print ('This is correct!')
score=score+1
print ("Your score so far is",score,"out of 10")
question=question+1
time.sleep(2)
print ('This is incorrect!')
question=question+1
time.sleep(2)
【问题讨论】:
-
请正确缩进您的代码。 Python 使用空格,而其他语言使用大括号。事实上,不可能确定代码块在代码中的位置。事实上,这甚至不应该运行,因为 while 循环不包含任何主体。
-
你为什么经常重复自己?
-
那么问题是什么?
-
是否曾经
operators(这是某种集合,因为您使用的是random.choice(operators))等于SINGLE 值? (您的operators == '*'测试检查的内容)
标签: python loops if-statement while-loop