-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
  • 109
  • 9

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

Dr.Strange Codes
  • 315
  • 1
  • 14
  • 1
    there's a slight flaw in this logic in that a negative number will not raise an exception, so stating that negatives are not allowed in the `except` statement is not technically accurate. – chitown88 Jun 09 '21 at 10:06
  • 2
    This also does not continue the loop if a string is inputted. – RealFishSam Jun 09 '21 at 10:06
  • Please check my edited answer, now if you put it in function it works. Code given in edited answer – Dr.Strange Codes Jun 09 '21 at 10:14
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
  • 24,774
  • 3
  • 26
  • 56