NameError : name "a" is not defined
NameError : name "a" is not defined
我打算在 Python 中制作一个 calculator.py 简单程序。
编译后,程序 returns 异常:"a" 未定义。我该如何解决?
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)
回溯(最后一次调用):
控制(a,x,y,z,k)
NameError:名称 'a' 未定义
那是因为在您 运行 控制 try 循环之前,您从未将 'a' 分配给任何东西。尝试在 运行 控制之前分配一些东西给 'a'。
try:
control(a, x, y, z, k)
我打算在 Python 中制作一个 calculator.py 简单程序。 编译后,程序 returns 异常:"a" 未定义。我该如何解决?
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)
回溯(最后一次调用): 控制(a,x,y,z,k) NameError:名称 'a' 未定义
那是因为在您 运行 控制 try 循环之前,您从未将 'a' 分配给任何东西。尝试在 运行 控制之前分配一些东西给 'a'。
try:
control(a, x, y, z, k)