简体   繁体   English

在while循环内无法通过while循环工作

[英]cant get a with a while loop inside a while loop to work

while loop == True:                                                                             #creates loop, and uses previously defined 'loop'
    try:
        ans = int(input("What is the answer to " + str(num1) + " x " + str(num2) + " ? "))      #asks question and requires a user input
        correct = (ans == num1 * num2)
        if correct:
            invtimer()
            print("You are correct! ")
            break                                                                               #if the answer is correct, it prints 'You are correct!' and breaks to avoid the loop
        else:
            invtimer()
   while loop == True:
            showans = input('''Wrong answer.
Would you like to see the answer? (yes)(y)
or try again? (no)(n) ''')
            if showans in ["yes", "y", "Yes", "Y"]:
                invtimer()
                print("The answer is " + str(num1 * num2) + "!")
                break
            elif showans in ["no", "n", "No", "N"]:
                loop == False
            else:
                print("That is not an option! ") 
                loop == False
    except ValueError:
        print("That is not a number! ") 

I need help with getting the second loop == False to link back the fist while loop. 我需要获得第二个循环== False的帮助才能将拳头的while循环连回。 when I run it, it keeps going back to the second while loop. 当我运行它时,它会一直回到第二个while循环。

You were using the == operator. 您正在使用==运算符。 This operator is only meant to be used in conditional statements and loops, not when assigning variables. 该运算符仅用于条件语句和循环,而不用于分配变量。 When you assign False to a variable use = instead 当您将False赋给变量时,请使用=代替

while loop == True:                                                                             #creates loop, and uses previously defined 'loop'
    try:
        ans = int(input("What is the answer to " + str(num1) + " x " + str(num2) + " ? "))      #asks question and requires a user input
        correct = (ans == num1 * num2)
        if correct:
            invtimer()
            print("You are correct! ")
            break                                                                               #if the answer is correct, it prints 'You are correct!' and breaks to avoid the loop
        else:
            invtimer()
        while loop == True:
            showans = input('''Wrong answer.
Would you like to see the answer? (yes)(y)
or try again? (no)(n) ''')
            if showans in ["yes", "y", "Yes", "Y"]:
                invtimer()
                print("The answer is " + str(num1 * num2) + "!")
                break
            elif showans in ["no", "n", "No", "N"]:
                loop = False
            else:
                print("That is not an option! ") 
                loop = False
    except ValueError:
        print("That is not a number! ") 

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

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