0

This is a simple code where the script would ask for the user's name, age, sex, and height and give an output based on that. My current code is as follows:

print "What is your name?"
name = raw_input()
print "How old are you?"
age = raw_input()
print "Are you male? Please answer Y or N"
sex = raw_input()
if sex == "Y" or sex == "y":
    sex = "Male"
else:
    sex = "Female"
print "How tall are you? Please enter in feet such as 5.5"
height = raw_input()

if sex == "Male" and height >= 6.0:
    t = "You are tall for a male"
elif sex == "Male" and height < 6.0:
    t = "You are below average for a male"

elif sex == "Female" and height >= 5.5:
    t = "You are tall for a female"
else:
    t = "You are below average for a female"


print "Hello %s. You are %s years old and %s feet tall. %s." % (name, age, height, t)

I am getting hung up on the if, elif, else statement:

if sex == "Male" and height >= 6.0:
    t = "You are tall for a male"
elif sex == "Male" and height < 6.0:
    t = "You are below average for a male"

elif sex == "Female" and height >= 5.5:
    t = "You are tall for a female"
else:
    t = "You are below average for a female"

The code will differentiate if sex is Male or Female, but will always return "You are tall for a xxx". I can not figure out how to get the "You are below average" return.

1 Answer 1

1

That's because raw_input() returns a string, not a float, and comparison is always the same way between a string and a float in Python 2.

>>> "1.0" > 6.0
True

Do this:

height = float(raw_input())

height = input() would have worked too but is discouraged (security issues because of evaluation)

Note: this has been fixed in Python 3 (probably because it wasn't very useful, and error-prone): trying to do this results in

TypeError: unorderable types: str() > float()

which is explicit and would have allowed to realize your mistake.

Note: same issue if you try to compare age (age = raw_input() should be age = int(raw_input()))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.