在python 3.4中,在全局部分的main函数中调用函数

在python 3.4中,在全局部分的main函数中调用函数,python,python-3.x,Python,Python 3.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(c) print (c) 回溯(最近一次呼叫最后一次):` 文件“C:/Users/Soham Pal/Desktop/dec_to_octal.py”,第8行,在 d=n根(c

错误是:

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)
回溯(最近一次呼叫最后一次):`
文件“C:/Users/Soham Pal/Desktop/dec_to_octal.py”,第8行,在
d=n根(c)
NameError:未定义名称“c”
问题是<代码>c不存在于函数之外

您可能希望尝试为函数提供正确的参数,但是,因为如果确实有
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

如果您将“x”输入的值指定给名为
x
的实际变量,这样您就可以理解它的用途了。既然您创建了一个包含两个参数的函数,那么调用它时应该传递两个参数。您的代码应该如下所示-

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)
现在,我们将从用户获取的两个值传递给函数(这里是a和b)。
函数是计算结果,将结果存储在c中,然后返回c。我们将此结果存储在d中。最后我们打印d.

那么,变量
c
是什么?它是在哪里定义的?
nthroot
接受两个参数并返回一个
c
(名称不重要)@ForceBru先生…实际上我是python新手…你能帮我运行上面的代码吗?行应该是
c=nthroot(a,b)
请阅读错误。您使用一些
c
变量调用了一个函数。我们想知道您认为您在哪里定义了它,因为它不在某个“全球部分”内
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)