-1

As seen in the code I am trying to convert the fee integer into a string to prevent a ValueError. I only want to display the message "That's not your real age mate," when the user inputs a negative number, a number >100, or a string.

active = True

while active:
    print("How old are you?")
    fee = int(input())

    #Checks age of user to determine the price of a movie ticket
    if 0 <= fee <= 3:
        print("You're free mate")
        active = False

    elif 4 <= fee <= 12:
        print("$10 please!")
        active = False

    elif 13 <= fee <= 64:
        print("$15 please!")
        active = False

    elif 65 <= fee <= 100:
        print("$10 please!")
        active = False

    #convert negative numbers or letters into strings as to not cause a ValueError $ restart the while loop
    else:
        fee = str(fee)
        print("That's not your real age mate")
        continue
realFishSam
  • 169
  • 14

2 Answers2

1

To fix value error put exception:-

active = True

try:
 while active:
    print("How old are you?")
    fee = int(input())

    #Checks age of user to determine the price of a movie ticket
    if 0 <= fee <= 3:
        print("You're free mate")
        active = False

    elif 4 <= fee <= 12:
        print("$10 please!")
        active = False

    elif 13 <= fee <= 64:
        print("$15 please!")
        active = False

    elif 65 <= fee <= 100:
        print("$10 please!")
        active = False

    #convert negative numbers or letters into strings as to not cause a ValueError $ restart the while loop
    else:
        fee = str(fee)
        print("That's not your real age mate")
        continue
      
except ValueError:
  print('Please type only numbers. Letters will not be allowed.')

If you use value-error exception it will be useful to send error message like: "Please type only positive numbers. Negative numbers and letters will not be allowed. So that people understand".

And if you want to continue loop after exception then put it in a function like this:-

def once_again():
  try:
   active = True
   while active:
      print("How old are you?")
      fee = int(input())

      #Checks age of user to determine the price of a movie ticket
      if 0 <= fee <= 3:
        print("You're free mate")
        active = False

      elif 4 <= fee <= 12:
        print("$10 please!")
        active = False

      elif 13 <= fee <= 64:
        print("$15 please!")
        active = False

      elif 65 <= fee <= 100:
        print("$10 please!")
        active = False

      #convert negative numbers or letters into strings as to not cause a ValueError $ restart the while loop
      else:
        fee = str(fee)
        print("That's not your real age mate")
        continue
      
  except ValueError:
    print('Please type only positive numbers. Negative numbers and letters will not be allowed.')
    once_again()
    
once_again()

Output:-

The output

0

Leave as string and only convert to an int by using .isdigit() to check if it's a digit

active = True

while active:
    print("How old are you?")
    fee = input()
    
    if not fee.isdigit():
        print("That's not your real age mate")
        continue
        
    fee = int(fee)

    #Checks age of user to determine the price of a movie ticket
    if 0 <= fee <= 3:
        print("You're free mate")
        active = False

    elif 4 <= fee <= 12:
        print("$10 please!")
        active = False

    elif 13 <= fee <= 64:
        print("$15 please!")
        active = False

    elif 65 <= fee <= 100:
        print("$10 please!")
        active = False

    #convert negative numbers or letters into strings as to not cause a ValueError $ restart the while loop
    else:
        print("That's not your real age mate")
        continue
chitown88
  • 27,527
  • 4
  • 30
  • 59