#๐ Help with formatting.
54 messages ยท Page 1 of 1 (latest)
@honest stump
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.
What do you mean by "format my function return"?
I would like it to only show to .2f when it prints my total price
:white_check_mark: Your 3.12 eval job has completed with return code 0.
1.23
!f-string
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.
ok i will try to implement
And FYI, this is printing, not returning. "Returning" is where you use the return statement.
print(f"Total Price: " + "$" + str(calcTax({subTotal:.2f}))) it is saying invalid decimal literal
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.
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
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().
ok will try
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.
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
What did you call the variable in calculate?
The error is telling you exactly what the problem is.
right and i thought it returned total from calcTax
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
1
Look at what you called the variable in calculate.
i see i have to define total again
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.
ok wonderful thank you again
You're welcome. You can !close if you're done with the thread.
!close
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.