I am trying to make a Shipping calculator in python by using IF Statements. The application should calculate the total cost of an order including shipping. I am stuck at a point where python just won't run my code at all. I have a feeling I'm close but I do not know what is wrong with what I'm inputting because python won't return any errors. please someone just steer me in the right direction.
Instructions:
Create a program that calculates the total cost of an order including shipping.
When your program is run it should ...
- Print the name of the program "Shipping Calculator"
- Prompt the user to type in the Cost of Items Ordered
- If the user enters a number that’s less than zero, display an error message and give the user a chance to enter the number again. (note: if you know loops/iterations, OK to use here, but if not, just a simple one-time check with an if statement will be OK here.) For example: if the cost is less than zero, print an error message and then re-ask once for the input.
Use the following table to calculate shipping cost: | Cost of Items | Shipping cost | | ------------- | ------------- | | <30 | 5.95 | | 30.00-49.99 | 7.95 | | 50.00-74.99 | 9.95 | | >75.00 | FREE |
- Print the Shipping cost and Total Cost to ship the item
- End the program with an exit greeting of your choice (e.g. Thank you for using our shipping calculator!)
Example 1:
================= Shipping Calculator
Cost of items ordered: 49.99
Shipping cost: 7.95
Total cost: 57.94
Thank you for using our shipping calculator!
Example 2:
================= Shipping Calculator
Cost of items ordered: -65.50
You must enter a positive number. Please try again.
Cost of items ordered: 65.50
Shipping cost: 9.95
Total cost: 75.45
Thank you for using our shipping calculator!
Here is the code I have written out so far:
#Assignment shipping calculator
#Author: Name
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping()
total = calc_total()
main()
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
if subtotal <= 0:
print("You must enter a positive number. Please try again.")
return subtotal
def calc_shipping():
subtotal = calc_subtotal()
shipping = calc_shipping
if subtotal >= 1 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
def calc_total():
total = calc_subtotal() calc_shipping()
print("Total Cost: ",total)
print("Thank you for using our shipping calculator!")
CodePudding user response:
First, I will put the code here then explain all the changes I made (your original attempt was very close, just a few tweaks):
#Assignment shipping calculator
#Author: Name
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping(subtotal)
calc_total(subtotal,shipping)
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
while subtotal <= 0:
print("You must enter a positive number. Please try again.")
subtotal = float(input("Cost of Items Ordered: $"))
return subtotal
def calc_shipping(subtotal):
if subtotal >= 1 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
return shipping
def calc_total(subtotal,shipping):
total = subtotal shipping
print("Total Cost: $" str(round(total,2)))
print("Thank you for using our shipping calculator!")
main()
As @Devang Sanghani mentioned, you should call your
main()
outside of your function so that it will runFor your
calc_subtotal()
function, use a while loop to keep taking in user input until given a valid number. Make sure you move yourreturn subtotal
outside of this loop so that it will only return it once this number is valid.In your
calc_shipping()
function, make sure you take insubtotal
as a parameter since you are using it in your function. Make sure you also returnshipping
.Similarly, in your
calc_total()
function, take in bothsubtotal
andshipping
as parameters since they are used in your function.Given these changes, update your
main()
function accordingly.
I hope these changes made sense! Please let me know if you need any further help or clarification :)
CodePudding user response:
So there seem to be 2 errors:
- You forgot to call the main function
- You indented the return statement in
calc_subtotal()
so it returns the value only if thesubtotal <= 0
The correct code might be:
def main():
print("Shipping Calculator")
subtotal = calc_subtotal()
shipping = calc_shipping()
total = calc_total()
def calc_subtotal():
subtotal = float(input("Cost of Items Ordered: $"))
if subtotal <= 0:
print("You must enter a positive number. Please try again.")
calc_subtotal() # Re asking for the input in case of invalid input
else:
return subtotal # returning subtotal in case of valid input
def calc_shipping():
subtotal = calc_subtotal()
shipping = calc_shipping
if subtotal >= 0 and subtotal <= 29.99:
shipping = 5.95
print("Shipping Cost: $5.95")
if subtotal >= 30 and subtotal <= 49.99:
shipping = 7.95
print("Shipping Cost: $7.95")
if subtotal >= 50 and subtotal <= 74.99:
shipping = 9.95
print("Shipping Cost: $9.95")
if subtotal >= 75:
shipping = 0
print("Shipping Cost: Free")
def calc_total():
total = calc_subtotal() calc_shipping()
print("Total Cost: ",total)
print("Thank you for using our shipping calculator!")
main() # Calling main
CodePudding user response:
Adding to the above answers, also consider the case where the price can be a few cents, so this line should change:
if subtotal >= 0 and subtotal <= 29.99: # changed from 1 to 0