Python Forum
input defines variable but it's not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input defines variable but it's not defined
#1
I am just starting to learn python, and trying out a basic "calculator" from a couple YouTube videos. I have spent the last few hours trying to figure out why a previous draft would only return "No comprende", only to have it suddenly start working. The current draft is returning an error after inputting the 2 numbers and a basic function, defining all variables to that point.
Error:
Traceback (most recent call last):  File "<file path>", line 31, in <module>    compute()  File "<file path>", line 19, in compute    if(var == 'add'): NameError: name 'var' is not defined
I am editing in IDLE and running in the Python 3.6.1 shell.
#basic math functions
def addit():
   return num1 + num2
def mult():
   return num1 * num2
def divide():
   return num1 / num2
def minus():
   return num1 - num2

#inputs for numbers and operation
def inputs():
   num1 = int(input("Pick a number"))
   num2 = int(input("Pick another number"))
   var = input("What do you want to do with these numbers? (add, subtract, multiply, or divide)")

#complete and display computation or invalid input
def compute():
   if(var == 'add'):
       print(addit())
   elif(var == 'subtract'):
       print(minus())
   elif(var == 'multiply'):
       print(mult())
   elif(var == 'divide'):
       print(divide())
   elif(var != 'add' and var != 'subtract' and var != 'multiply' and var != 'divide'):
       print("No comprende")

#calls functions and do again
inputs()
compute()
moar = input("again? yes or no")
if(moar == 'yes'):
   inputs()
   compute()
After reading through a couple other threads, I added a return to the inputs function, but it had no effect.
def inputs():
   num1 = int(input("Pick a number"))
   num2 = int(input("Pick another number"))
   var = input("What do you want to do with these numbers? (add, subtract, multiply, or divide)")
   return(num1,num2,var)
Anyone able to point out my problem other than being green as grass and thick as a stump?
Reply
#2
If you define a variable in a function, it only exists in that function (this is called scope). To get it out of that function, you need to use a return statement. To get it into another function, you use parameters to the function call. Details on all of this are in the function tutorials linked to in my signature.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jun-04-2017, 11:40 AM)tozqo Wrote:    elif(var != 'add' and var != 'subtract' and var != 'multiply' and var != 'divide'):
This should just be else. If control has reached the bottom of that if condition and has already compared to those strings then its not any of those strings and there is no need to redo the condition.

Quote:To get it out of that function, you need to use a return statement. To get it into another function, you use parameters to the function call
this is one reason why one would use a class in some cases as they no longer have to pass a variable back and forth....but you should first get a handle on how to do that before tackling classes.

And thank you for reading what to include in a post before your first post. It is pleasant to read posts when it is short and sweet, but yet not vague and gives enough information to allow a response instead of digging for more info. As well as a short description of attempts to fix. I wish more beginner posts were of this nature.
Recommended Tutorials:
Reply
#4
Thanks for the tips and info. I currently work at a help desk, so 5 step troubleshooting is life. I've had to deal with bad documentation way too many times to make that mistake when seeking help.

So, working variables in multiple functions has 2 steps, returning the variable from one function, and calling it in the function that needs it. Can I call the parameters globally and then use that one call for all functions?
Reply
#5
That's a bad idea. You want to avoid globals.
The proper way to do it is return the the objects needed by the calling routine.
In your case, at the end of the def inputs() function add:
   return num1, num2, var
and when calling:
    num1, num2, var = inputs()
Reply
#6
Thanks for the globals advice. I added the calls, found out that it was doing the input() function twice(once for the global and once for the compute() call) and fixed that and found out that the variables don't use the same name across functions, but they do use the same input order.

changing from "num1, num2, var = inputs()"
to "var, num1, num2 = inputs()" always returns "No comprende" because var is first variable pulled by the compute() function, and is a number. Also, fiddling around with if's got different outputs. First, I had to change my statements from add(), etc. to add(num1, num2), etc. Leaving the () empty returned nothing, and the order of what I had in () changed the output. ex: if subtract() was changed to subtract(num1, num2) it returned num1 - num2; but if I changed to subtract(num2, num1) it returned num2 - num1, because the input order for the subtract() function was reversed.

I haven't read through all of the function tutorial in ichabod801's link, so I don't know if that is mentioned there yet.

I think I've gotten everything for this sorted out that I needed. Thanks very much for the help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 109 Yesterday, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,165 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  input variable choice MCL169 7 1,116 Feb-19-2023, 09:00 PM
Last Post: MCL169
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,044 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  How to include input as part of variable name Mark17 4 2,444 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Function will not return variable that I think is defined Oldman45 6 3,435 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  trying to input a variable using random.choice python63 9 3,530 Aug-13-2020, 05:37 PM
Last Post: python63
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,164 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 2,491 Jul-10-2020, 11:27 PM
Last Post: Heyjoe

Forum Jump:

User Panel Messages

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