简体   繁体   中英

Python is repeating my statement in a while loop

I created this program to check if the user inputs a number. When they input a number, the program runs smoothly. However, when a user does not input a number, the program repeatedly posts "Invalid. Enter a Decimal number please," and does not allow for the loop to repeat until a number is inputted. How can I modify this?

def validate_number(user_input):
  while True:
    try:
        user_input = int(user_input)
        break
    except ValueError:
        print()
        print("Invaild. Enter a decimal number please.")

budget = input('What is your budget: $')
validate_number(budget)

plane_tickets = input("How much are the plane tickets in USD: $")
validate_number(plane_tickets)

hotel = input("How much is the hotel in USD: $")
validate_number(hotel)

you need to get the user input again... so let's change the validate_number() function a little bit to allow for this:

def validate_number(user_input):
  while True:
    try:
        user_input = int(user_input)
        return user_input
    except ValueError:
        print()
        print("Invaild. Enter a decimal number please.")
        user_input = input()

It seems that what you are trying to achieve is to force the user to enter a valid integer. So let us take a slightly different approach to your code.

The function integer_input takes two (string) variables: input_message and invalid_input_message . It asks for user input using the passed input_message then tries cast the input value to an integer and return it in the line return int(val) . If type casting raised the ValueError the invalid_input_message get printed out and the user is asked for input again because of the infinite loop.

Now we use this function instead of the simple input() function. budget = integer_input() instead of budget = input()

def integer_input(input_message, invalid_input_message='invalide'):
    while True:
        try:
            val = input(input_message)
            return int(val)
        except ValueError:
            print(invalid_input_message)

budget = integer_input('What is your budget: $', 'Invaild. Enter a decimal number please.')
plane_tickets = integer_input('How much are the plane tickets in USD: $', 'Invaild. Enter a decimal number please.')
hotel = integer_input('How much is the hotel in USD: $', 'Invaild. Enter a decimal number please.')

I changed your function to return the number after the while break, obviously this will print your error if the input contains a char

def validate_number(text):
    while True:
        try:
            question = int(input(text))
            break
        except:
            print("That's not a valid option!")
     
    return question 

budget  = validate_number('What is your budget: $')

plane_tickets = validate_number("How much are the plane tickets in USD: $")

hotel = validate_number("How much is the hotel in USD: $")  

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