Python Forum
NameError x not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError x not defined
#1
Hello, my class just began functions() and defining them but I'm stuck rn.

def greeting():
    print("Hello, welcome to the Calculator App")


def menu():
    decide=input("1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division")

def num():
    num1=input("Enter first value")
    num2=input("Enter second value")

def calc():
    if decide == "1":
        ans = num1 + num2
        print(ans)
    if decide == "2":
        ans2 = num1 - num2
        print(ans2)
    if decide == "3":
        ans3 = num1 * num2
        print(ans3)
    if decide == "4":
        ans4 = num1 / num2
        print(ans4)





greeting()

menu()

num()

calc()
When I run this part of code, it yields a "decide" is not defined. I'm not sure what to do as to fix this. Also, I'm confused as to what the parenthesis next to a function should contain, what could and should be added inside the parenthesis?
Reply
#2
That's because it's out of scope by the time you call calc
You can fix this by returning the value, example:
def menu():
    decide=input("1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division")
    return decide
and when you call menu:
decide = menu()
You will need to fix the other functions as well

you will then have to modify calc to accept the arguments, decide, num, etc.
and pass those values to decide.
Reply
#3
(Feb-27-2019, 06:08 AM)Larz60+ Wrote: That's because it's out of scope by the time you call calc You can fix this by returning the value, example:
 def menu(): decide=input("1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division") return decide 
and when you call menu:
 decide = menu() 
You will need to fix the other functions as well you will then have to modify calc to accept the arguments, decide, num, etc. and pass those values to decide.


How would I accept the arguments in my calc()? And how would I pass those values down to decide? Sorry for all the questions I'm just really confused, first month of programming and I'm still wrapping my head around even just indentation
Reply
#4
The first line of calc would look like this:

def calc(decide, num1, num2):
There is a brief function tutorial on this site.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Feb-27-2019, 06:34 AM)ichabod801 Wrote: The first line of calc would look like this:
 def calc(decide, num1, num2): 
There is a brief function tutorial on this site.
So you're calling num1 and num2 to the calc() function? Is it because otherwise I would get a define error for those two if I didn't put them in the parameter?
Reply
#6
You should read the brief tutorial that ichabod801 shows above it will help
here's an example that might help:

>>> def choose_color(choice):
...     hue = ['red', 'yellow', 'green', 'blue']
...     return hue[choice]
... 
>>> def paint_my_car(color):
...     print('I will paint my car {}'.format(color))
... 
>>> paint_my_car(choose_color(0))
I will paint my car red
>>> paint_my_car(choose_color(2))
I will paint my car green
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation NameError: name 'score' is not defined - Help? MrKnd94 13 4,507 Mar-06-2023, 10:54 PM
Last Post: deanhystad
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 11,279 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 4,682 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 6,203 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  NameError: name 'print_string' is not defined jamie_01 2 2,029 Jun-11-2020, 05:27 AM
Last Post: buran
  "NameError: name 'catName1' is not defined tofif 3 5,627 Jun-24-2019, 06:05 AM
Last Post: perfringo
  NameError: name 'mailbox_list' is not defined pythonnewb 2 4,729 Aug-06-2017, 09:31 PM
Last Post: pythonnewb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020