If no correct elif, python uses the first elif even if it’s incorrect? /u/HoskinZo Python Education

I’m new to python (reading Python Crash Course by Eric Matthes and loving it)–I just learned about if elif and else. I learned you can use if then multiple elif statements, which it will evaluate in order and only execute the first one that is correct. But if none are correct, why does it still execute the first elif? Did I make a mistake? Shouldn’t it show some kind of error?

I modified the example in the book to try it out:

age = 67 if age < 4: price = 0 elif age < 18: price = 25 elif age < 40: price = 40 elif age < 65: price = 20 print(f'Your entrance fee cost is ${price}.') 

The output I got was: Your entrance fee cost is $25.

Did I made a mistake? Why did it do something where the condition was false?

Thank you!

submitted by /u/HoskinZo
[link] [comments]

​r/learnpython I’m new to python (reading Python Crash Course by Eric Matthes and loving it)–I just learned about if elif and else. I learned you can use if then multiple elif statements, which it will evaluate in order and only execute the first one that is correct. But if none are correct, why does it still execute the first elif? Did I make a mistake? Shouldn’t it show some kind of error? I modified the example in the book to try it out: age = 67 if age < 4: price = 0 elif age < 18: price = 25 elif age < 40: price = 40 elif age < 65: price = 20 print(f’Your entrance fee cost is ${price}.’) The output I got was: Your entrance fee cost is $25. Did I made a mistake? Why did it do something where the condition was false? Thank you! submitted by /u/HoskinZo [link] [comments] 

I’m new to python (reading Python Crash Course by Eric Matthes and loving it)–I just learned about if elif and else. I learned you can use if then multiple elif statements, which it will evaluate in order and only execute the first one that is correct. But if none are correct, why does it still execute the first elif? Did I make a mistake? Shouldn’t it show some kind of error?

I modified the example in the book to try it out:

age = 67 if age < 4: price = 0 elif age < 18: price = 25 elif age < 40: price = 40 elif age < 65: price = 20 print(f'Your entrance fee cost is ${price}.') 

The output I got was: Your entrance fee cost is $25.

Did I made a mistake? Why did it do something where the condition was false?

Thank you!

submitted by /u/HoskinZo
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *