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


Messages In This Thread
input defines variable but it's not defined - by tozqo - Jun-04-2017, 11:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 345 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 624 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,386 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  input variable choice MCL169 7 1,222 Feb-19-2023, 09:00 PM
Last Post: MCL169
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,180 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  How to include input as part of variable name Mark17 4 2,544 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Function will not return variable that I think is defined Oldman45 6 3,574 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  trying to input a variable using random.choice python63 9 3,683 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,316 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 2,599 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