-1

I've been trying to make my simple code work and searching different methods of checking to see if a variable is a certain type when using if statements

# Are you tall enough to ride the roller coaster

print('This will determine if you are able to ride this Rollar Coaster?')
age = int(input('How old are you? '))
if isinstance(age, int):
    print('Please input a valid age')
elif age <= 12:
    print('You are not old enough to ride this ride!')
else:
    height = int(input('How tall are you? Enter in centimeters please: '))
    if height > 72:
        print('You may enter, enjoy the ride')
    else:
        print('You are not tall enough to ride.')

I've searched and googled stack overflow and i came across the isinstance and issubclass and they dont seem to work. I also tried while != int although I'm not entirely sure that code works.

Godfist
  • 1
  • 3
  • 4
    There is already an error in case of wrong input.`ValueError: invalid literal for int() with base 10: (your input)` Which will be triggered for the line where you assign `age`. – Shadowcoder Jan 31 '21 at 18:20
  • 1
    Your current check is redundant, age is forced to be an int in the line above. You should take advantage of the exception raised by that line (see comment from Shadowcoder) and catch it in a Try Except block – Simon Notley Jan 31 '21 at 18:25
  • See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). You look up how to do it. This question is answered in several places on Stack Overflow, and many on the Internet in general. – Prune Jan 31 '21 at 18:29

3 Answers3

0
age = int(input('How old are you? '))

Here, the input function always returns a string and using int() you are converting the string input to an integer. So, now age is an integer. Hence, the following if condition will always return true

if isinstance(age, int):
    print('Please input a valid age')

and therefore, your program would end up asking for valid input even if it receives valid input.

ubaid shaikh
  • 453
  • 3
  • 7
-1

you can use try-except to check for errors if try code has an error then except is executed, it is almost like if else

print('This will determine if you are able to ride this Rollar Coaster?')
try:
    age = int(input('How old are you? '))
    if age <= 12:
        print('You are not old enough to ride this ride!')
    else:
        height = int(input('How tall are you? Enter in centimeters please: '))
        if height > 72:
            print('You may enter, enjoy the ride')
        else:
            print('You are not tall enough to ride.')

except:
    print('Please input a valid age')
Pratik Agrawal
  • 405
  • 3
  • 17
-1

Corrected code :

# Are you tall enough to ride the roller coaster

print('This will determine if you are able to ride this Rollar Coaster?')
try:
    age = int(input('How old are you? '))
    if age <= 12:
        print('You are not old enough to ride this ride!')
    else:
        height = int(input('How tall are you? Enter in centimeters please: '))
        if height > 72:
            print('You may enter, enjoy the ride')
        else:
            print('You are not tall enough to ride.')
except ValueError:
    print("Please enter valid age.")
except Exception as e:
    print(e) # Other exceptions

Explanation : In your code age = int(input('How old are you? ')) <-- here you are already trying to convert the input to an integer. Hence if the input is not an integer type, it will throw an error. So, it is better to use the try exception to handle such situation.

Pranbir Sarkar
  • 111
  • 1
  • 10