Jun-04-2017, 11:40 AM
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?