简体   繁体   中英

How to check user input for multiple conditions within same loop or function in Python 3?

I'm trying to take user-input but I need the input to be an integer, AND be between 1 and 9. I tried putting "in range(1,10)" in a few places in the code but it didn't work. I need the program to keep asking the user for the right input until they give the correct input. So far I've only been able to make sure their input is an integer with he following code. I will be taking input by using int(input("...")) , rather than using input("...") .

while True:
    try:
        ui1 = int(input("Player 1, Your move. Select your move. "))
        break
    except ValueError:
        print("You have to choose a number between 1 and 9")
        continue

Why not just check isdigit() and in range ?

while True:
    ui1 = input("Player 1, Your move. Select your move. ")
    if ui1.isdigit() and int(ui1) in range(1,10):
        break
    print("You have to choose a number between 1 and 9")

# Continue code out of the loop

Add a check before the break and move the error message to the end of the loop.

while True:
    try:
        ui1 = int(input("Player 1, Your move. Select your move. "))
        if 1 <= ui1 <= 9:
            break
    except ValueError:
        pass
    print("You have to choose a number between 1 and 9")
# beJeb
# Stack overflow -
# https://stackoverflow.com/questions/51202856/how-to-check-user-input-for-multiple-conditions-within-same-loop-or-function-in

# Our main function, only used to grab input and call our other function(s).
def main():

    while True:   
        try:
            userVar = int(input("Player 1, Your move. Select your move: "))
            break
        except ValueError:
            print("Incorrect input type, please enter an integer: ")

    # We can assume that our input is an int if we get here, so check for range
    checkRange = isGoodRange(userVar)

    # Checking to make sure our input is in range and reprompt, or print.
    if(checkRange != False):
        print("Player 1 chooses to make the move: %d" %(userVar))
    else:
        print("Your input is not in the range of 1-9, please enter a correct var.")
        main()

# This function will check if our number is within our range.
def isGoodRange(whatNum):
    if(whatNum < 10) & (whatNum > 0):
        return True

    else: return False

# Protecting the main function
if __name__ == "__main__":
    main()

Note: I tested a couple inputs so I believe this should be enough to help you understand the process, if not please comment, message, etc. Also, if this answer helps you, please select it as answered to help others.

Let the number be the input you are taking from the user.

while(1): #To ensure that input is continuous
    number = int(input())
    if number>=1 and number<=10 and number.isdigit():
        break #if the input is valid, you can proceed with the number
    else:
        print("Enter a valid Number")

The number can be used for further operations.

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