0

I'm quite a newbie to programming and python. Though, I have taken a few lessons at www.udacity.com and www.codecademy.com.

I have a problem with this nested if/else statement towards the bottom of the code block, the "nested" if statement is running but the elif statement after that is not running as it was supposed to when an Age value of less than 18 is entered. I get no error messages when I run this in the Python IDLE which is v. 2.5.4 leading me to believe that there is a problem similar to that of white space where the if statement does not expect an else or elif statement and thinks that the code ends with that nested if statement.

The last output is the first of the two print statements in the nested if/else statement.

I have searched for answers to my problem but none of the sources I find give me the answer to this specific problem and I can't figure out what is wrong with the code. Perhaps, I'm just blind.

print("Welcome")

Name = raw_input("Enter your name ")

print("Hello, " + Name + ".")

Age = raw_input("Enter your age ")

Gender = raw_input("Gender? ")

if Gender == "M" or "Male" or "male" or "boy" or "Boy":
    if Age >= 18:
        print ("You are " + Name + ", a " + Age + " year old man.") 
    elif Age < 18:
        print ("You are " + Name + ", a " + Age + " year old boy.")

2 Answers2

3

raw_input returns text, you can't sensibly* do this:

Age (text) < 18 (number):

You need to convert Age to a number, e.g.

Age = raw_input("Enter your age ")
Age = int(Age)

(* although you are allowed to do that, so it's not an error to Python, it just doesn't do what you want)

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
-1

Below is a working example. You are taking your age in as a string and testing it compared to 18. This will almost always deliver the value true and display you are a male to fix this, you can take the age in as a int before you test the value.

Also when testing for the gender, you can create the same result by testing if the all upper case version of your input is equal to M or MALE and it is much easier for the user as you have removed the case sensitivity of their input.

print("Welcome")

Name = raw_input("Enter your name ")

print("Hello, " + Name + ".")

Age = input("Enter your age ")

Gender = raw_input("Gender? ")

if Gender.upper() == "M" or Gender.upper() == "MALE":
    if Age >= 18:
        print ("You are " + Name + ", a " + str(Age) + " year old man.") 
    elif Age < 18:
        print ("You are " + Name + ", a " + str(Age) + " year old boy.")
Ryan
  • 1,972
  • 2
  • 23
  • 36
  • `if Gender.upper() == "M" or "MALE"` isn't as useful of a condition as it seems. – TigerhawkT3 Nov 21 '15 at 02:00
  • please explain, it removes the need for testing all cases. – Ryan Nov 21 '15 at 02:02
  • Thank You, this helped clear up things for me and the code is also working as intended now. I should've thought about the inputs themselves and the conditions I gave rather than focusing on the statements and syntax. – AceVariable Nov 21 '15 at 02:03
  • The problem is that it always evaluates to `True`. It is, in fact, the very error that's solved in the linked duplicate you didn't approve of. – TigerhawkT3 Nov 21 '15 at 02:04
  • Thank you for your comment, i haven't avidly coded in python in a while and i have forgotten some things as i have moved away from the language. I appreciate the remark it has been fixed. – Ryan Nov 21 '15 at 02:11