Reputation: 53
I'm working on a little program where the user inputs a price and then the program would output the shipping cost based on the input.
#Initilaize variables
lowUS = 6.00;
medUS = 9.00;
highUS = 12.00;
lowCan = 8.00;
medCan = 12.00;
highCan = 15.00;
#Greet user and ask for input
print("Welcome to Ben's shipping calculator!");
print("We will calculate your shipping cost for you!");
orderTotal = float(input("Please input your total amount."));
country = input("In which country do you reside? Please type C for Canada or U or USA. ");
#Validate input
while country not in {'u', 'c', 'C', 'U'}:
print ("Invalid input. Please try again. ")
country = input("In which country do you reside? Please type C for Canada or U or USA. ");
#Determine how much the shipping fee is
if country == 'U' or country == 'u':
if orderTotal <= 50.00:
if orderTotal > 50.00 and orderTotal <= 100.00:
if orderTotal > 100.00 and orderTotal <= 150.00:
if orderTotal > 150.00:
print("Your shipping is free and your grand total is", orderTotal)
else:
print("Your shipping fee is: ", highUS);
orderTotal = (orderTotal + highUS);
else:
print("Your shipping fee is: ", medUS);
orderTotal = (orderTotal + medUS);
else:
print("Your shipping fee is: ", lowUS);
orderTotal = (orderTotal + lowUS);
elif country == 'c' or country == 'C':
if orderTotal <= 50.00:
if orderTotal > 50.00 and orderTotal <= 100.00:
if orderTotal > 100.00 and orderTotal <= 150.00:
if orderTotal > 150.00:
print("Your shipping is free and your grand total is", orderTotal)
else:
print("Your shipping fee is: ", highCan);
orderTotal = (orderTotal + highCan);
else:
print("Your shipping fee is: ", medCan);
orderTotal = (orderTotal + medCan);
else:
print("Your shipping fee is: ", lowCan);
orderTotal = (orderTotal + lowCan);
print("Your grand total is: $", orderTotal);
I am very new to python and programming and I'm not sure if this is a good way to go at it, but I'm learning if-else statements so I thought I would give it a try. So far it only works if you input "50" for the amount. It will calculate based off of the country only for "50". I'm not sure where I went wrong, if someone could help and explain, that would be great.
Upvotes: 0
Views: 61
Reputation: 1524
Your logic is a bit screwy. If the amount is <= $50, then it is not > $50 or <= $100, so anything following that nested "if" will never execute:
if orderTotal <= 50.00:
if orderTotal > 50.00 and orderTotal <= 100.00:
# ** this will never run **
else:
# This will run, if orderTotal <= 50.00
Nothing will happen if orderTotal > 50.00
, because there's no else
for the if orderTotal <= 50.00
test. @Jon Clements' answer shows the correct way to structure your code.
Upvotes: 0
Reputation: 142156
Your first if
only enters if it's less than or equal to 50... the next check you make is if it's greater than 50 which it can't be - so that block will never execute... So your else
clause is the only one that can execute... Basically, your nested if
statements won't execute because the criteria for doing so is already excluded from the enclosing if
.
You're best off restructuring your logic as:
if orderTotal > 150:
# do something
elif orderTotal > 100:
# do something
elif orderTotal > 50:
# do something
else: # 50 or under...
# do something else
Upvotes: 1