-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
calculating the shipping order #12
Comments
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 ***@***.***> 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
<#12>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOMH7JJJCN5DZ44RNF54BEDSB554RANCNFSM4QH7HLBQ>
.
|
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 orderexample 1shippingFee = False orderTotal = float(input("Enter the total amount of order: ")) if orderTotal <= 50: # changed >= to <= else: sumTotal = orderTotal + shippingFee print("Your total shipping fee with order is ${0:.2f}".format(shippingFee)) print() Example 2 (If/Else Statement) shipping fee to orderuse simple If Else statementsExample 2sumTotal = 0 orderTotal = float(input("Please enter total amount of order: ")) if orderTotal <= 50: else: sumTotal = orderTotal + shipFee print("Your order amount is: ${0:.2f}".format(orderTotal)) print() Thanks! |
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 |
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))
The text was updated successfully, but these errors were encountered: