Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python if elif else statement

Tags:

I'm trying to create a program with python that calculate the cost for shipping.

However, I can't run the program to where it works properly.

What ever my total is the same amount comes out as $6 for US and $8 for Canada. I can't seem to get pass that.

total = raw_input('What is the total amount for your online shopping?') country = raw_input('Shipping within the US or Canada?')  if country == "US":     if total <= "50":         print "Shipping Costs $6.00"     elif total <= "100":             print "Shipping Costs $9.00"     elif total <= "150":             print "Shipping Costs $12.00"     else:         print "FREE"  if country == "Canada":     if total <= "50":         print "Shipping Costs $8.00"     elif total <= "100":         print "Shipping Costs $12.00"     elif total <= "150":         print "Shipping Costs $15.00"     else:         print "FREE" 
like image 878
sakefon Avatar asked Oct 15 '13 00:10

sakefon


People also ask

What is if-Elif-else statement in Python?

If-elif-else statement is used in Python for decision-making i.e the program will evaluate test expression and will execute the remaining statements only if the given test expression turns out to be true. This allows validation for multiple expressions.

How do we use the IF-Elif-else statement?

The if-elif-else statement is used to conditionally execute a statement or a block of statements. Conditions can be true or false, execute one thing when the condition is true, something else when the condition is false.

What is the difference between if-else and if-Elif-else statement?

The first form if-if-if test all conditions, whereas the second if-elif-else tests only as many as needed: if it finds one condition that is True, it stops and doesn't evaluate the rest. In other words: if-elif-else is used when the conditions are mutually exclusive. Hope this answers your question!!!

Do you need an else statement with if and Elif?

IF, ELSE or ELIF (known as else if in some programming) are conditional statements which are used for execution of different code depends on condition. The if statements can be written without else or elif statements, But else and elif can't be used without else.


2 Answers

  1. you should get integer from raw_input, not string. use int().
  2. comparison values like 50, 100, 150, ... also should be integer.

below is fixed code.

total = int(raw_input('What is the total amount for your online shopping?')) country = raw_input('Shipping within the US or Canada?')  if country == "US":     if total <= 50:         print "Shipping Costs $6.00"     elif total <= 100:         print "Shipping Costs $9.00"   # improved indentation     elif total <= 150:         print "Shipping Costs $12.00"  # improved indentation     else:         print "FREE"  if country == "Canada":     if total <= 50:         print "Shipping Costs $8.00"     elif total <= 100:         print "Shipping Costs $12.00"     elif total <= 150:         print "Shipping Costs $15.00"     else:         print "FREE" 
like image 183
Curry Avatar answered Oct 05 '22 23:10

Curry


You can't compare Strings numerically. Instead convert to an int first and then compare.

For example:

if int(total) < 50 

Variables to avoid duplication would help too.

like image 33
Jeanne Boyarsky Avatar answered Oct 05 '22 23:10

Jeanne Boyarsky