Introduction-Programming-Python icon indicating copy to clipboard operation
Introduction-Programming-Python copied to clipboard

calculating the shipping order

Open mcbrighty opened this issue 4 years ago • 3 comments

I decided to add Boolean variable to this code of solving it and whenever I ran my code I get additional $1 added if the total shipping order exceeds or equals $50. Why? This is the code:

Program to calculate a shipping order and give a tip of free shipping if order exceeds $50 else $10 fee

shippingFee = False sumTotal = 0

orderTotal = float(input("Enter the total amount of order ")) if orderTotal >= 50: shippingFee = True print("Shipping is free")

else: shippingFee = 10 print("Shipping fee will cost additional $10")

sumTotal = orderTotal + shippingFee

print("Your total shipping fee with order is $%2f" % sumTotal) print("Your total shipping fee with order is $" + str(sumTotal))

mcbrighty avatar Aug 22 '20 08:08 mcbrighty

Hi,

Unfortunately Python is very loose structured language. You used shippingFee variable as a Boolean first then if the total amount less than $50 you used same variable as integer and kept shipping fee.

In Python it is possible but not best practice.

Please be careful when you set shippingFee = True it is logically “1”

And when you sum like

sumTotal = orderTotal + shippingFee

You are adding +1 to your orderTotal.

I am checked your code with my old iPhone and can’t see indentations properly but I am guessing.

Try to calculate sumTotal separately in each of block or else block

Or

Try to use different variable for different type.

Stay safe.

On Sat, 22 Aug 2020 at 10:22, mcbrighty [email protected] wrote:

I decided to add Boolean variable to this code of solving it and whenever I ran my code I get additional $1 added if the total shipping order exceeds or equals $50. Why?

This is the code:

Program to calculate a shipping order and give a tip of free shipping if order exceeds $50 else $10 fee

shippingFee = False

sumTotal = 0

orderTotal = float(input("Enter the total amount of order "))

if orderTotal >= 50:

shippingFee = True

print("Shipping is free")

else:

shippingFee = 10

print("Shipping fee will cost additional $10")

sumTotal = orderTotal + shippingFee

print("Your total shipping fee with order is $%2f" % sumTotal)

print("Your total shipping fee with order is $" + str(sumTotal))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GeekTrainer/Introduction-Programming-Python/issues/12, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOMH7JJJCN5DZ44RNF54BEDSB554RANCNFSM4QH7HLBQ .

izleogrenkodla avatar Aug 23 '20 11:08 izleogrenkodla

There are few issues with your code for the results that your seeking @mcbrighty .

First, as @izleogrenkodla stated, in Boolean "True" has a value of "1", and the way Boolean is used in the code, the results simply added the value "1". In addition, the Boolean is being used backward, so the results would only add value of 1 for orders over 50 while adding value 10 for orders under 50. This occurs because: in code "if orderTotal >= 50:" when it should be "if orderTotal <= 50:" but still that would not change the fact that True adds the value "1" that required ensure appropriate variable added (see below example). There are a couple of smaller coding issues. Below I have provided two example codes, the first uses Boolean which will provide the results. But with the simple results that is being seek, I would use the "If/Else Statement which is cleaner and easier to use with numerical values--the second example code. Hopefully, they have been helpful. Thanks.

Example 1 (Boolean)

shipping fee to order

example 1

shippingFee = False sumTotal = 0

orderTotal = float(input("Enter the total amount of order: "))

if orderTotal <= 50: # changed >= to <= shippingFee = True shippingFee = 0 # added this variable to add 0 instead 1 print("Shipping is free")

else: shippingFee = 10 print("Shipping fee will cost additional $10")

sumTotal = orderTotal + shippingFee

print("Your total shipping fee with order is ${0:.2f}".format(shippingFee)) print("Your total order cost is ${0:.2f}".format(sumTotal))

print()

Example 2 (If/Else Statement)

shipping fee to order

use simple If Else statements

Example 2

sumTotal = 0

orderTotal = float(input("Please enter total amount of order: "))

if orderTotal <= 50: shipFee = 0 print("Shipping is Free!") print()

else: shipFee = 10 print("Shipping fee will cost additional $10") print()

sumTotal = orderTotal + shipFee

print("Your order amount is: ${0:.2f}".format(orderTotal)) print("You shipping cost is: ${0:.2f}".format(shipFee)) print("You total order cost is: ${0:.2f}".format(sumTotal))

print()

Thanks!

pjk302 avatar Aug 24 '20 01:08 pjk302

Sorry, indentation required in the appropriate sections, the formatting of this comment section does a poor job of reflecting actual code~ I could provide actual py files if necessary. Thanks! Patrick

pjk302 avatar Aug 24 '20 01:08 pjk302