NameError : name "a" is not defined
Question:
I have made a simple calculator.py program in Python, but the program fails with NameError: "a" is not defined.
How can I fix it?
import math
def control(a, x, y, z, k):
return {
'ADDITION': addition(x, y),
'SUBTRACTION': subtraction(x, y),
'MULTIPLICATION': multiplication(x, y),
'DIVISION': division(x, y),
'MOD': modulo(x, y),
'SECONDPOWER': secondPower(x),
'POWER': power(x, y),
'SECONDRADIX': secondRadix(x),
'MAGIC': magic(x, y, z, k)
}[a]
def addition(x, y):
return float(x) + float(y)
def subtraction(x, y):
return float(x) - float(y)
def multiplication(x, y):
return float(x) * float(y)
def division(x, y):
return float(x) / float(y)
def modulo(x, y):
return float(x) % float(y)
def secondPower(x):
return math.pow(float(x),2.0)
def power(x, y):
return math.pow(float(x),float(y))
def secondRadix(x):
return math.sqrt(float(x))
def magic(x, y, z, k):
l = float(x) + float(k)
m = float(y) + float(z)
return (l / m) + 1.0
try:
control(a, x, y, z, k)
except ValueError:
print("This operation is not supported for given input parameters")
out = control(a, x, y, z, k)
print(out)
Traceback (most recent call last):
control(a, x, y, z, k)
NameError: name 'a' is not defined
Answers:
That’s because before you run control in the try loop, you’ve never assigned ‘a’ to anything. Try assigning something to ‘a’ right before running control.
try:
control(a, x, y, z, k)
You need to assign values not just to the variable a but also to other variables x, y, z, and k which you’re passing here -> control(a, x, y, z, k)
# for example:
a= 'ADDITION'
x= 1
y= 2
z=3
k=4
# and then you can call the function
control(a, x, y, z, k)
and the value of the variable a must be one of these(as you’ve defined)
'ADDITION'
'SUBTRACTION'
'MULTIPLICATION'
'DIVISION'
'MOD'
'SECONDPOWER'
'POWER'
'SECONDRADIX'
'MAGIC'
any other value of a will through a "KeyError"
I have made a simple calculator.py program in Python, but the program fails with NameError: "a" is not defined.
How can I fix it?
import math
def control(a, x, y, z, k):
return {
'ADDITION': addition(x, y),
'SUBTRACTION': subtraction(x, y),
'MULTIPLICATION': multiplication(x, y),
'DIVISION': division(x, y),
'MOD': modulo(x, y),
'SECONDPOWER': secondPower(x),
'POWER': power(x, y),
'SECONDRADIX': secondRadix(x),
'MAGIC': magic(x, y, z, k)
}[a]
def addition(x, y):
return float(x) + float(y)
def subtraction(x, y):
return float(x) - float(y)
def multiplication(x, y):
return float(x) * float(y)
def division(x, y):
return float(x) / float(y)
def modulo(x, y):
return float(x) % float(y)
def secondPower(x):
return math.pow(float(x),2.0)
def power(x, y):
return math.pow(float(x),float(y))
def secondRadix(x):
return math.sqrt(float(x))
def magic(x, y, z, k):
l = float(x) + float(k)
m = float(y) + float(z)
return (l / m) + 1.0
try:
control(a, x, y, z, k)
except ValueError:
print("This operation is not supported for given input parameters")
out = control(a, x, y, z, k)
print(out)
Traceback (most recent call last):
control(a, x, y, z, k)
NameError: name 'a' is not defined
That’s because before you run control in the try loop, you’ve never assigned ‘a’ to anything. Try assigning something to ‘a’ right before running control.
try:
control(a, x, y, z, k)
You need to assign values not just to the variable a but also to other variables x, y, z, and k which you’re passing here -> control(a, x, y, z, k)
# for example:
a= 'ADDITION'
x= 1
y= 2
z=3
k=4
# and then you can call the function
control(a, x, y, z, k)
and the value of the variable a must be one of these(as you’ve defined)
'ADDITION'
'SUBTRACTION'
'MULTIPLICATION'
'DIVISION'
'MOD'
'SECONDPOWER'
'POWER'
'SECONDRADIX'
'MAGIC'
any other value of a will through a "KeyError"