简体   繁体   中英

Proper Validation Loops with Spyder 4.1.4 and python 3.8

so my code is this:

def main(): 
    print("Press '1' to start a new dice roll calculation") 
    print("or press '0' to end the program") #escape key
    validation = int(input()) #so the user can press enter
    while validation != 1 or validation != 0: #input validation loop with escape key
        print("Press ENTER to start a new dice roll calculation")
        validation = int(input())
    

when I try to run the validation loop for some reason it throws me into an infinite loop, but when I checked my textbook (Tony Gaddis, 2017) it should be correct. Where am I wrong?

It is checking to see if either 1 or 0 doesn't exist. if you type 1, 0 doesn't exist and vice versa, so it will always cause an error. Change the or to an and like this:

def main(): 
    print("Press '1' to start a new dice roll calculation") 

    print("or press '0' to end the program") #escape key

    validation = int(input()) #so the user can press enter

    #input validation loop with escape key using AND instead of OR
    while validation != 1 and validation != 0: 
        print("Press ENTER to start a new dice roll calculation")
        validation = int(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