roui living
roui living

Reputation: 3

Can someone explain why elif is being ignored?

I'm very new to programming can't wrap my head around why my elif statements are being ignored. Can anyone help?

prompt = ("Enter your age to buy a ticket ")
prompt += ("or type 'quit' to end the program:")

while True:
    age = raw_input(prompt)

    if age == 'quit':
        break
    elif age < 3:
        print "Free ticket."
    elif age < 12:
        print "$10 ticket."
    else:
        print"$15 ticket."

Upvotes: 0

Views: 314

Answers (2)

Sraw
Sraw

Reputation: 20264

I will tell you some basic debug skills.

You want to know why elif is ignored means you want to know why elif block is not entered. And the reason is obvious, condition is False.

So you can simply output the condition to checkout. print age < 3, and it will output False.

Upvotes: 1

Cov
Cov

Reputation: 572

age is a string. Convert it to an int before checking for integers.

prompt = ("Enter your age to buy a ticket ")
prompt += ("or type 'quit' to end the program:")

while True:
    age = raw_input(prompt)

    if age == 'quit':
        break
    elif int(age) < 3:
        print "Free ticket."
    elif int(age) < 12:
        print "$10 ticket."
    else:
        print "$15 ticket."

Upvotes: 1

Related Questions