简体   繁体   中英

How to use and debug conditional statements in Python?

while True:

    n1 = int(input("Enter first number: "))
    if n1>255:
               print("Invalid input. Sorry the number should be less than 255.")
               continue
    elif n1<255:
               print("The input is valid.")


    n2 = int(input("Enter second number: "))
    if n2>255:
               print("Invalid input. Sorry the second number should be less than 255.")
               continue

    elif n2<255:
               print("The input is valid.")

    break

Whenever I enter 'n2' higher than 255, it shows enter first number. I want it to show enter second number.

在此处输入图片说明

The problem is the continue statement in the first if . continue does not mean "go on". It means, "ignore the rest of the while/for loop, and repeat from its next iteration". So, actually, the next time you are at the input prompt, are not entering n2 , you are again the the same line and entering n1 again. Remove the continue statement.

Take a look at how break and continue statements work, they are pretty much the same in all languages (that I know of at least)

continue starts the whole while loop over: it doesn't go back to before the last if statement or anything like that. So your second continue effectively starts your program over. You need to get rid of this continue , and use some other method to redo this input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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