NameError:名称"a"没有定义 [英] NameError : name "a" is not defined

查看:59
本文介绍了NameError:名称"a"没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要用Python编写一个Calculator.py简单程序.编译后,程序返回Exception:未定义"a".我该如何解决?

I'm going to make a calculator.py simple program in Python. After compiling, program returns Exception : "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)

回溯(最近通话最近):控制(a,x,y,z,k)NameError:名称"a"未定义

Traceback (most recent call last): control(a, x, y, z, k) NameError: name 'a' is not defined

推荐答案

这是因为在try循环中运行控件之前,您从未为任何事物分配"a".尝试在运行控件之前为"a"分配一些内容.

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)

这篇关于NameError:名称"a"没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆