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"
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.
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.
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!!!
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.
int()
.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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With