-1

I have this code:

active = True
print("Hello, this is the pricing system for the theater.")
print("Depending on your age, your pricing will vary.")
print("Please type 'exit' to termiante the program")
toddler = "Below 3 = Free." 
kid = "3-2 = $10"
big_kid = "12+ = $15"
prompt = "Please enter your age: "

while active:
    message = input(prompt)
    if message == "exit":
        break
    if message != type(int):
        print("Int only")
        continue
    message = int(message)

    if message < 3:
        print(toddler)
    elif message > 3 and message < 12:
        print(kid)
    else:
        print(big_kid)

I am basically trying to tell python that if the value that was entered is not an integer, to go back to the beginning and input an int. However, even if the user does put an int, it keeps telling me "int only". What am I doing wrong here?

Shlomi U
  • 41
  • 1
  • 8
  • 3
    This code: `if message != type(int)` is never going to work. It's comparing the value of message against the type of int. – jarmod May 11 '22 at 17:56
  • How can I do that? if I try to test for message in a later part of the loop it just keeps telling me that message is not defined. – Shlomi U May 11 '22 at 17:57
  • Hint: `print(message)`. Then `print(type(int))` and compare them. You basically want `type(message)` but as already pointed out, it will always be `str` by definition. – tripleee May 11 '22 at 17:59

3 Answers3

1

try to use message.isnumeric() instead of checking it with != operator

1

The condition type(int) can't be true, as you have an str from input, you'd rather try str.isdigit()

while True:
    message = input(prompt)
    if message == "exit":
        break
    if message.isdigit():
        print("Int only")
        continue
        
    message = int(message)
    if message < 3:
        print(toddler)
    elif 3 <= message < 12:
        print(kid)
    else:
        print(big_kid)
azro
  • 53,056
  • 7
  • 34
  • 70
1

You can use try-except block also.

active = True
print("Hello, this is the pricing system for the theater.")
print("Depending on your age, your pricing will vary.")
print("Please type 'exit' to termiante the program")
toddler = "Below 3 = Free." 
kid = "3-2 = $10"
big_kid = "12+ = $15"
prompt = "Please enter your age: "

while active:
    message = input(prompt)
    print(message)
    if message == "exit":
        break
    try:
        int(message)
    except ValueError:
        print("Int only")
        continue
    message = int(message)

    if message < 3:
        print(toddler)
    elif message > 3 and message < 12:
        print(kid)
    else:
        print(big_kid)

codester_09
  • 5,622
  • 2
  • 5
  • 27