在python3.4中,调用全局部分的main函数

2024-10-02 08:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

import math
def nthroot(x,n):
    c=math.pow(x,1/n)
    return(c)

a=float(input("enter the value of x"))
b=float(input("enter the value of n"))
d=nthroot(c)
print (c)

错误是:

Traceback (most recent call last):`
  File "C:/Users/Soham Pal/Desktop/dec_to_octal.py", line 8, in <module>
    d=nthroot(c)
NameError: name 'c' is not defined

Tags: oftheimportinputreturnvaluedef错误
2条回答

问题是variable scopec在函数外部不存在

您可能希望尝试为函数提供正确的参数,但是如果确实有c变量,则会出现不同的错误

import math
def nthroot(x,n):
    return math.pow(x,1/n)

a=float(input("enter the value of x"))
b=float(input("enter the value of n"))

d=nthroot(a, b)  # See here
print(d)

如果您将“x”输入的值赋给一个名为x的实际变量,这样您就可以理解它的用途,这可能会有所帮助

因为您创建了一个接受两个参数的函数,所以在调用它时应该传递两个参数。你的代码应该是-

import math

def nthroot(x,n):
    c=math.pow(x,1/n)
    return(c)
a=float(input("enter the value of x"))
b=float(input("enter the value of n"))
d=nthroot(a,b)
print (d)

现在,我们将从用户获取的两个值传递给函数(这里是a和b)。 函数用于计算结果,将结果存储在c中,并返回c。我们将这个结果存储在d中。最后我们打印d

相关问题 更多 >

    热门问题