简体   繁体   English

while 循环中的三个 If 语句

[英]Three If statements in while loops

I have three if statements in my while loop that is supposed to ask questions until the question variable is equal to 10.我的 while 循环中有三个 if 语句,它们应该在问题变量等于 10 之前提出问题。

However I think because I have three if statements is the reason why it is only asking three questions instead.但是我认为因为我有三个 if 语句是它只问三个问题的原因。 I have tried to use break - which ends my while loop我尝试使用 break - 这会结束我的 while 循环

This is my code:这是我的代码:

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) 
while question<10: 
   user_answer=input(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers1))) 

   if operators == '+': 
       expected_answer = numbers + numbers1

   if operators == '-': 
      expected_answer = numbers - numbers1

   if operators == '*': 
      expected_answer = numbers * numbers1

   if user_answer==expected_answer:
       score=score+1
       print ('This is correct!')
   else: 
       print ('This is incorrect!')

   print ("Your score so far is",score,"out of 10")
   question=question+1 
   time.sleep(2) 

Can be done much better, mapping the operators and so.可以做得更好,映射运算符等等。 I'll let you do it :)我会让你做的:)

The loop runs less than 10 times because if the answer is correct, the question variable is increased twice.循环运行不到 10 次,因为如果答案正确,问题变量会增加两倍。 And both messages ("correct"/"incorrect") are printed.并且打印两条消息(“正确”/“不正确”)。 Your code needs 'else':您的代码需要“其他”:

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) 
else:
    print ('This is incorrect!') 
    question=question+1 
    time.sleep(2) 

But consider to rewrite the whole code as stated by Anton Epikhin!但是考虑按照 Anton Epikhin 的说法重写整个代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM