-3

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)
h.avery
  • 9
  • 3
  • What are you trying to achieve...? – Ouss Feb 11 '21 at 17:27
  • @Ouss I'm trying to check if the user input is an integer. If not, I want to print "Invalid" and continue prompting the user to input a decimal number before moving on to the next questions – h.avery Feb 11 '21 at 17:28
  • I see. I edited my answer below. You need to set user_input=input() after exception – Ouss Feb 11 '21 at 17:37
  • Also see [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Feb 11 '21 at 17:48

3 Answers3

1

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()
Ouss
  • 2,912
  • 2
  • 25
  • 45
  • The program crashed. The Errror message given is "Traceback (most recent call last): File "main.py", line 81, in validate_number(budget) File "main.py", line 73, in validate_number user_input = int(user_input) TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'" – h.avery Feb 11 '21 at 17:21
  • You are right. I edited the answer. please test and confirm ! – Ouss Feb 11 '21 at 17:49
0

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.')
Ouss
  • 2,912
  • 2
  • 25
  • 45
  • ok, but you never called Invalid_input_message – h.avery Feb 11 '21 at 17:55
  • The new function takes two (string) variables: `input_message` and `invalid_input_message`. If the exception ValueError was raised, the invalid_input_message is printed out. – Ouss Feb 11 '21 at 18:04
0

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: $")  
Joaquín
  • 350
  • 2
  • 12