#đź”’ So I have a prompt that prompts you for your location it will change depending on the users input
172 messages · Page 1 of 1 (latest)
@obsidian moat
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.
the issue is that you never defined a variable called amount_of_tax
or you can say that it doesnt exist in the code because you didn’t make them yet
your function takes input for something that didn’t exist
so that’s why it showed that error
specifically inside main, you're trying to reference the variables amount_of_tax and subtotal which weren't defined inside of main
if you define a variable inside a function, you can't view the value of that variable outside of that function
(simplifying a bit)
well to fix it, you would have to make a variable called amount_of_tax
with initial value of something, perhaps 0
that would allow you to do operations with it
but you’ll need to define “subtotal” too
wouldnt that be a global variable thogh
oh subtotal is defined in first function
if you put it in the main() function, no. it would be a local variable to that function
if you define it outside that function, yes, it would be global
okay i did some fixing of the code
sorry i know its a screenshot
how would i grab the subtotal from above and get it for def Show_final_total
if you want to access a variable declared inside a function, you need to return that value from the function
so you already return the value from Paintoption
you just never access it
inside your main you want subtotal = Paintoption()
just note that you don't always return from inside of Paintoption, so sometimes you won't get the expected result
inside of every if statement you need a return
or you need a return at the end of the function
Oh so after the ifs id need to return amount_of_tax?
amount of tax is already defined as 0 so there is no need
Do this in the PaintOption function
for subtotal
so at the end of def Paintoption(): i already have return(subtotal)
is that what you mean
sort of
that is at the end
that return is inside an if statement
but in the statement of if
so technically it’s not end of function, but end of the if statement
you need to take that return subtotal outside of the if statement
oh so i take away the indentation?
!e
def function():
if 3+4 ==7:
A = 7
return A
B = function()
print(B)
@raw blaze :white_check_mark: Your 3.12 eval job has completed with return code 0.
7
yeah and put it outside of the if statement like this
im sorry i am trying to understand i just dont know exactly where i am to put it
here so you see what im dealing with
do you know how if statements work in python?
i mean did i use the ifs wrong
do you know how functions and return statements work?
just asking because it changes the explanation, and it's related to the problem
so return sends the results back
?
it ends the function and sends the results back
yeah exactly
so i need to put a return at the end of the if statements?
so in Paintoption you're almost there, but do you see the issue with your return statement?
it's nested inside of an if statement
so you only return from your function if Paintoption == 2
but you want to return always
so how can you modify your return statement so it's not a part of that if block
taking away the indent?
yep, exactly
so bring it to the left side and do 4 spaces?
you should try testing out your solution to see what the right indent is
yeah that's the right indent
no, that function is fine
where does the error message say that the error is coming from?
specifically related to the return statement. the return is fine there
line 84 so that means in my last def Show_final_total
the subtotal is not being brought there?
that's what the error message implies, yeah
so if i add def Show_final_total(final_amount,amount_of_tax,)
subtotal inside the parathesis
and put subtotal in the main Show_final_total()
it executes the whole thing but it makes the values 0
yeah so you've resolved all the errors, but now you have some logic issues
specifically the pattern in the code i see is not using the results of function calls
so first line of Show_final_total and your call to Paintoption() inside of main
you call these functions, but why? they return a value, but what do you do with that value?
the value from paintoption is subtotal right
like the whole reason for the paint option function is to find subtotal
so i want it to call that value back?
not call back
but you need to assign the return value to a variable
foo = Paintoption()
thats in main?
yeah so in your main right now you have ```py
amount_of_tax=0
subtotal=0
but subtotal isn't zero. it's the result of Paintoption
OH I SEE
let's go
amount_of_tax is actually superfluous here
in the sense that it's never not 0 and it never affects any of your calculations
you can probably just remove it
though perhaps that points to a bug?
can't really comment on the logic -- you may need a new function if you do still want amount_of_tax to be a non-zero value
yeah for this i need amount of tax to be shown in the end
i have another issue too
now that i run the program it like duplicates things and asks for options it didnt need before
are you still calling addTaxTosubtotal twice (once inside main and againinside Show_final_total)
oh and now you're calling Paintoption() twice
dont i need Paintoption() as function 2 so the program would work
oh wait
no
I hope you know you have taught me more than my prof has
so it like display the amount of tax in the final amount part but not by itself in amount_of_tax
hm you mean this print? ```
The amount of tax is: 0
The final amount is: (400.0, 2000)
the first value is 0 and the second row contains the right value?
yeah exactly
i just changed it
so like in return(amount_of_tax,subtotal)
theres a + sign between them now
so thats good now its just why is amount of tax not showing up there
so when you have return (x, y)
you're actually returning multiple values
which is why, if you look at the print i sent, you get (400.0, 2000)
there's two values there
i assume you want to keep doing something similar
addTaxTosubtotal() returns two values -- the updated subtotal and the amount of tax applied
(alternatively you can just keep the old value and calculate the tax using subtraction)
but to access those two values you need to "unpack" them x, y = addTaxTosubtotal()
oh i suppose you never modify subtotal
instead you can do this ```py
def addTaxTosubtotal(...):
...
return amount_of_tax
def main():
...
subtotal = Paintoption()
tax_amount = addTaxTosubtotal(subtotal)
final_amount = subtotal + tax_amount
Show_final_total(final_amount,amount_of_tax)
how many arguments does addTaxTosubtotal expect?
its using subtotal to determine the amount of tax
no because its finding that
yep
its using taxoption
did you change the function to only expect 1 argument?
oh i hope i'm not being too curt
you're doing fine. it's pretty common to not notice stuff like that when you're starting out
You're not being curt, you're connor.
:D
i appreciate you man
my prof is not very helpful it sucks i had to resort to asking a discord member, but im glad this discord is a thing and yall are here willing to help
i really learned alot today python can be tricky but its good to know when you are doing something wrong and why its happening
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.