#๐Ÿ”’ Help with formatting.

54 messages ยท Page 1 of 1 (latest)

honest stump
#

I cannot figure out the correct way to format my function return.

sly perchBOT
#

@honest stump

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.

honest stump
full peak
honest stump
#

I would like it to only show to .2f when it prints my total price

full peak
#

You can use f-strings:

#

!e

price = 1.23456
print(f"{price:.2f}")
sly perchBOT
full peak
#

!f-string

sly perchBOT
#
Format-strings

Creating a Python string with your variables using the + operator can be difficult to write and read. F-strings (format-strings) make it easy to insert values into a string. If you put an f in front of the first quote, you can then put Python expressions between curly braces in the string.

>>> snake = "pythons"
>>> number = 21
>>> f"There are {number * 2} {snake} on the plane."
"There are 42 pythons on the plane."

Note that even when you include an expression that isn't a string, like number * 2, Python will convert it to a string for you.

honest stump
#

ok i will try to implement

full peak
#

And FYI, this is printing, not returning. "Returning" is where you use the return statement.

honest stump
#

print(f"Total Price: " + "$" + str(calcTax({subTotal:.2f}))) it is saying invalid decimal literal

full peak
#

Yes. Note the f in my example

#

Oh wait

#

This code doesn't look like it would run even before that error.

#

The {} isn't in a string

#

You want f"Total Price: ${subTotal:.2f}"

#

The purpose of f-strings is so you don't need the +s over and over.

#

Well, one of the purposes.

honest stump
#

as a total noob thank you it is working as it should now

#

mine was so messy lol

#

Oh and one other thing, how can I make it wait for the user to press enter when prompted

#

right now it just runs main again

#

when it finishes

full peak
#

You're welcome.

And I'm not sure what you mean. input will always wait until the user presses enter unless something weird is going on with the input stream.

#

Oh, I see.

#

Just put input().

honest stump
#

ok will try

full peak
#

I'd just let the function return back to main, since it will re-show the menu and wait for input at the input line anyway.

honest stump
#

gotcha

#

on line 59 I had subTotal instead of total so I changed it

#

oh wait i might know

#

i added a calcTax call line after the elifs and still no luck

full peak
#

What did you call the variable in calculate?

#

The error is telling you exactly what the problem is.

honest stump
#

right and i thought it returned total from calcTax

full peak
#

The name returned by a function doesn't matter

#

!e

def func():
    some_name = 1
    return some_name

some_other_name = func()
print(some_other_name)
sly perchBOT
full peak
#

Look at what you called the variable in calculate.

honest stump
#

i see i have to define total again

full peak
#

Yes, or just use subTotal, since that's what you were using before.

#

Well, actually I guess you weren't. I misremembered the code.

#

So ya, either create a new total variable that holds the return of calcTax, or just += onto subTotal.

honest stump
#

ok wonderful thank you again

full peak
#

You're welcome. You can !close if you're done with the thread.

honest stump
#

!close

sly perchBOT
#
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.