-3

This is the code:

age = input("How Old Are You?")
gender = str(input("What is your gender (Please no caps)"))

if isinstance(age,int):
    age = int(age)
else:
    print("Enter a valid age")

age = int(age)

if gender == "male":
    if age < 30:
        print("Watch Captain America")
    elif age > 30:
        print("Watch Johnny English")
    else:
        print("Watch Iron Man")
elif gender == "female":
    if age < 30:
        print("Watch Frozen")
    elif age > 30:
        print("Watch Cinderella")
    else:
        print("Watch Fox and the Hound")
else:
    print("Enter a Valid Gender")

This program recommends a movie for you to watch, based on age and gender.

I am not sure what the error is. This is written in Python 3.5.1 and does not work on Python 2.x.

Edit

Sorry, I did not explain it well enough, what I am trying to do is that if the User enters a character, then there is no error message, but they are prompted to try again.

That is the use of this block, which I cannot work out:

if isinstance(age,int):
    age = int(age)
else:
    print("Enter a valid age")
Community
  • 1
  • 1

1 Answers1

1

This block is unnecessary and confusing:

if isinstance(age,int):
    age = int(age)
else:
    print("Enter a valid age")

At the point of you receiving age, it is always going to be a string, so this will always print "Enter a valid age".

What you want to do is the cast to int when you get the input. You can also remove the redundant cast to str on your other input since they default to string.

age = int(input("How Old Are You?"))
gender = input("What is your gender (Please no caps)")

Otherwise, from what I can tell your program works fine (in spite of the "error" message).

Makoto
  • 104,088
  • 27
  • 192
  • 230