Home > Software engineering >  Python Shipping Calculator Using IF Statements
Python Shipping Calculator Using IF Statements

Time:02-23

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 ...

  1. Print the name of the program "Shipping Calculator"
  2. Prompt the user to type in the Cost of Items Ordered
  3. 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 |

  1. Print the Shipping cost and Total Cost to ship the item
  2. 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()
  1. As @Devang Sanghani mentioned, you should call your main() outside of your function so that it will run

  2. For your calc_subtotal() function, use a while loop to keep taking in user input until given a valid number. Make sure you move your return subtotal outside of this loop so that it will only return it once this number is valid.

  3. In your calc_shipping() function, make sure you take in subtotal as a parameter since you are using it in your function. Make sure you also return shipping.

  4. Similarly, in your calc_total() function, take in both subtotal and shipping as parameters since they are used in your function.

  5. 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:

  1. You forgot to call the main function
  2. You indented the return statement in calc_subtotal() so it returns the value only if the subtotal <= 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
  • Related