0

So I'm fairly new to python and for my programming class, I have to write a program about a 100 metre race and tells if you qualified or not based on the time it took you to finish. If you're a male and you took longer than 10.18 seconds to finish; then you didn't qualify. If you're a female and it took you longer than 11.29 seconds to finish; then again, you didn't qualify.

My problem is that both messages saying if you qualified or didn't qualified appear no matter what your time was. I am using Python 2.7.10. My code so far is:

gender = raw_input("Are you Male (M) or Female (F)?: ")
time = raw_input("What time did you get for the 100m race?: ")


if gender is "M" and time > 10.18:
    print "Sorry, you did not qualify"
else:
    print "Congratulations, you qualified!"

if gender is "F" and time > 11.29:
    print "Sorry, you did not qualify"
else:
    print "Congratulations, you qualified!"
1
  • before asking these questions, I beg of the OP(s) to please run them in a debugger - the answer will leap out to you. Commented Oct 2, 2015 at 15:11

4 Answers 4

2

Raw_input returns a string. You need to do time = float(raw_input("What time..."))

(Note that python will allow you to compare a string to a float, but it doesn't try to convert the string to match)

(Edit: And as noted by the other two answers at the time of this posting, you should use elif)

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

Comments

1

Try using elif for better handling

if gender is "M" and time > 10.18:
    print "Sorry, you did not qualify"
elif gender is "F" and time > 11.29:
    print "Sorry, you did not qualify"
else:
    print "Congratulations, you qualified!"

Comments

0

The else for each clause will always run because gender will always be opposite to what the user has inputted. You also need to cast the 2nd input to float to correctly compare the value to either 10.18 or 11.29.

To correct this (without refactoring):

gender = raw_input("Are you Male (M) or Female (F)?: ")
time = float(raw_input("What time did you get for the 100m race?: "))

if gender is "M" and time > 10.18:
    print "Sorry, you did not qualify"    
elif gender is "F" and time > 11.29:
    print "Sorry, you did not qualify"
else:
    print "Congratulations, you qualified!"

8 Comments

The description is incorrect about the whole "because gender will always be opposite" notion, but correct that you have to cast to float (try '1.2' > 1.3 to see). The proposed code does work.
How is it incorrect? Within the original script each else would run because if the user was to enter M, the 2nd if statement is incorrect, thus the 2nd else statement would run. Vice versa if the user was to enter F, the first if statement is invalid so the first else statement would run.
What about any case where the runner's time was high? In that case, one or the other 'if' comes out true (assuming the user correctly typed 'M' or 'F', of course).
I don't understand what you mean by 'high'? This script doesn't handle validation of M / F inputted either so, we'll ignore that.
"High" means over the limit for qualifying. Try either "F" or "M" with a time of 20 seconds. Actually try it (I did) -- it is not true that both "else"s run.
|
0

Take the logic step-by-step. Let's consider an example of a Female with a time of 10 seconds:

The first 'if' comes out False, because she's female (False AND anything is still False, so the time doesn't even matter). So the first "Sorry" message doesn't get printed.

But since that 'if' was not executed, the 'else' immediately following does get executed, printing the message.

That's the problem: Just because someone is not a Male who failed, doesn't mean that they are a Male who succeeded. The female we're using as an example is neither.

Then, after incorrectly printing that message, it tries all over again for the female-who-failed case, and prints the message you did want.

You need to make the logic of the program, exactly match the logic of the real-life situation. So think in detail about which decisions affect what other decisions in this case.

I'll leave the exact change up to you, since that's likely what your teacher wants you to work through and figure out.

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.