#đź”’ 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
#

I am trying to get the amount of tax variable and plug it into my equation for my final amount.
NameError: name 'amount_of_tax' is not defined............... im not sure how to define it when it is changing depending on the users input
heres my code :https://paste.pythondiscord.com/TS4A

blazing girderBOT
#

@obsidian moat

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.

raw blaze
#

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

subtle crag
#

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)

raw blaze
#

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

obsidian moat
#

wouldnt that be a global variable thogh

subtle crag
#

i think he wants to have subtotal = Paintoption()

#

what would be a global variable?

obsidian moat
#

if i put amount_of_tax = 0 into the main

#

like at the top

raw blaze
#

oh subtotal is defined in first function

subtle crag
#

if you define it outside that function, yes, it would be global

obsidian moat
#

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

subtle crag
#

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

obsidian moat
#

Oh so after the ifs id need to return amount_of_tax?

raw blaze
#

amount of tax is already defined as 0 so there is no need

raw blaze
#

for subtotal

obsidian moat
#

so at the end of def Paintoption(): i already have return(subtotal)

#

is that what you mean

subtle crag
#

sort of

raw blaze
#

that is at the end

subtle crag
#

that return is inside an if statement

raw blaze
#

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

obsidian moat
#

oh so i take away the indentation?

raw blaze
#

!e
def function():
if 3+4 ==7:
A = 7
return A
B = function()
print(B)

blazing girderBOT
#

@raw blaze :white_check_mark: Your 3.12 eval job has completed with return code 0.

7
raw blaze
obsidian moat
#

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

subtle crag
#

do you know how if statements work in python?

obsidian moat
#

i mean did i use the ifs wrong

subtle crag
#

do you know how functions and return statements work?

#

just asking because it changes the explanation, and it's related to the problem

obsidian moat
#

so return sends the results back

#

?

#

it ends the function and sends the results back

subtle crag
#

yeah exactly

obsidian moat
#

so i need to put a return at the end of the if statements?

subtle crag
#

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

obsidian moat
#

taking away the indent?

subtle crag
#

yep, exactly

obsidian moat
#

so bring it to the left side and do 4 spaces?

subtle crag
#

you should try testing out your solution to see what the right indent is

obsidian moat
#

when i have it here it works for both paint options

subtle crag
#

yeah that's the right indent

obsidian moat
#

so now i do that with addTaxTosubtotal too?

#

the def addTaxTosubtotal

subtle crag
#

no, that function is fine

#

where does the error message say that the error is coming from?

subtle crag
obsidian moat
#

line 84 so that means in my last def Show_final_total

#

the subtotal is not being brought there?

subtle crag
#

that's what the error message implies, yeah

obsidian moat
#

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

subtle crag
#

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?

obsidian moat
#

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?

subtle crag
#

not call back

#

but you need to assign the return value to a variable

#

foo = Paintoption()

obsidian moat
#

thats in main?

subtle crag
#

where do you need the return value?

#

where do you call Paintoption?

obsidian moat
#

ah man

#

to get subtotal?

subtle crag
#

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

obsidian moat
#

OH I SEE

subtle crag
#

let's go

obsidian moat
#

so then for amount of tax

#

it would need a whole new function to call

subtle crag
#

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

obsidian moat
#

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

subtle crag
#

are you still calling addTaxTosubtotal twice (once inside main and againinside Show_final_total)

#

oh and now you're calling Paintoption() twice

obsidian moat
#

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

subtle crag
#

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?

obsidian moat
#

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

subtle crag
#

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)

obsidian moat
#

i dont know why its throwing this error now when its in there

subtle crag
#

how many arguments does addTaxTosubtotal expect?

obsidian moat
#

2

#

so i have to have the other one in there

subtle crag
#

well, does it need 2 arguments?

#

is the function using both arguments?

obsidian moat
#

its using subtotal to determine the amount of tax

subtle crag
#

and what about the second argument

#

it's using subtotal

#

is it using amount_of_tax

obsidian moat
#

no because its finding that

subtle crag
#

yep

obsidian moat
#

its using taxoption

subtle crag
#

so you only need 1 argument

#

is taxoption an argument?

obsidian moat
#

no

#

i have subtotal in there but its just throwing that error

subtle crag
#

did you change the function to only expect 1 argument?

obsidian moat
#

im so dumb

#

no

#

its working now

burnt geyser
#

You're not dumb, you're just learning.

#

don't be too hard on yourself

subtle crag
#

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

burnt geyser
#

You're not being curt, you're connor.

subtle crag
#

lmfao

#

boo

burnt geyser
#

:D

obsidian moat
#

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

blazing girderBOT
#
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.