#๐Ÿ”’ How do I edit my code to not allow partial pounds

50 messages ยท Page 1 of 1 (latest)

lofty spruceBOT
#

@covert mist

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

strong belfry
#

๐Ÿค”

covert mist
#

One sec ๐Ÿ˜ญ

strong belfry
#

ยฃ

covert mist
#

It wont let me upload my code

novel isle
#

!paste

lofty spruceBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

covert mist
#

#Get the number of pounds from the user input

Use If else statements to calculate the total_payable

#Calculate the cost_of_coffee sales_tax shipping_fee and total_amount

#Display the result

#Sets the sales_tax variable

sales_tax = .07

#gets the number of pounds based on the user input
pounds = float(input("How many pounds are you ordering?"))

#sets the shipping price = pounds]
shipping_price = pounds

#sets pound_price variable
pound_price = '';

if pounds > 150:
shipping_price = 0

if pounds >= 1 and pounds <= 9:
pound_price = (12.00 * pounds)

if pounds >= 10 and pounds < 20:
pound_price = (10.00 * pounds)

if pounds >= 20 and pounds < 40:
pound_price = (8.75 * pounds)

if pounds >= 40:
pound_price = (7.50 * pounds)

#calculates the sales tax
sales_tax = (pound_price * sales_tax)

total_cost = (pound_price + sales_tax + shipping_price)

print(f'Cost of coffee ${pound_price:.2f}')
print(f'7% sales tax ${sales_tax:.2f}')
print(f'Shipping fee ${shipping_price:.2f}')
print(f'Toal cost ${total_cost:.2f}')

strong belfry
#

maybe pounds = round(float(input("How many pounds are you ordering?"))) ?

covert mist
#

Where in the code would that go?

pastel roost
#

Please paste the code so we can provide help

covert mist
#

Im trying to

pastel roost
#

!paste

lofty spruceBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

covert mist
#

!paste

strong belfry
pastel roost
#

Click on the link

covert mist
#

I did and pasted the code

pastel roost
#

Copy the link

#

Put it here

covert mist
pastel roost
#

In hr browser that You pasted ur code in

#

Where is Your proplem

covert mist
#

I do not know how to make the code to where it does not allow partial pounds

strong belfry
#

pretty sure I've already told you

leaden imp
#

op if by partial pounds if u mean, u dont want the user to enter float numbers then just take integers from them

pounds = int(input("How many pounds are you ordering?"))
strong belfry
#
sales_tax = 0.07

pounds = round(float(input("How many pounds are you ordering?")))

shipping_price = pounds

pound_price = ""


if pounds > 150:
    shipping_price = 0

if pounds >= 1 and pounds <= 9:
    pound_price = 12.00 * pounds

if pounds >= 10 and pounds < 20:
    pound_price = 10.00 * pounds

if pounds >= 20 and pounds < 40:
    pound_price = 8.75 * pounds

if pounds >= 40:
    pound_price = 7.50 * pounds

sales_tax = pound_price * sales_tax

total_cost = pound_price + sales_tax + shipping_price

print(f"Cost of coffee ${pound_price:.2f}")
print(f"7% sales tax ${sales_tax:.2f}")
print(f"Shipping fee ${shipping_price:.2f}")
print(f"Toal cost ${total_cost:.2f}")

# echo 12.2 | python3 shiesty.py
# How many pounds are you ordering?Cost of coffee $120.00
# 7% sales tax $8.40
# Shipping fee $12.00
# Toal cost $140.40
pastel roost
#

Int(input(ur input))

#

Like that

#

Or checking by if and else

#

Which is stupid to do ngl

#

If You want the user to try again in the input without shutting the code

#

You can use try:

#

Example:
While true:
try:
Pounds = input(โ€œur inputโ€)
If pounds == int
Return pounds
Except:
Input(โ€œplease try againโ€)

true island
#

To add on to what georgino said:

# while loop if you want to have the user retry, if not you can omit this
while True:
    pounds = float(input("How many pounds are you ordering? "))
    if not int(pounds) == pounds:
        pounds = float(input("Sorry, partial pounds are not allowed, please enter a whole number:"))
    else:
        break
        
# option 2: raise an exception
pounds = float(input("How many pounds are you ordering? "))
if not int(pounds) == pounds:
    raise ValueError("Sorry, partial pounds are not allowed.")
# this will exit your script forcefully if the exception is not caught, probably not what you would want here...```
pastel roost
#

You should have said input not float input

#

Cause a float input would force them to make a float

#

Which is partial

true island
#

No, that is intentional. Input always returns a string and if you dont convert it to a numerical value first we cant check if it's a whole number. (well you could do "." in str, but that seems a little bit convoluted to me when you can just use numerical values)

pastel roost
#

Both of them work

#

Even of the input is a string

#

If*

#

You can check by try and except value error

#

Am i right?

true island
#

If input is a string this line if not int(pounds) == pounds: will throw an exception for non int values, so it wouldn't throw your own exception. That is why I converted it to a float first.
Yes you could just as well use a try/except block for this. ๐Ÿ™‚

pastel roost
#

Ok!

#

All of us are yapping here lol

lofty spruceBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.