Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 函数名称未定义错误_Python 3.x_Runtime Error - Fatal编程技术网

Python 3.x 函数名称未定义错误

Python 3.x 函数名称未定义错误,python-3.x,runtime-error,Python 3.x,Runtime Error,我想用python编写一个函数,告知我给出的数字是否为素数,目前为止我的代码是: def enter_input(): n=input("Enter the number to be checked\n") return n def check(): o=int(enter_input()) r=o print(type(o)) print(type(r)) bool=True for i in range(2,r,1):

我想用python编写一个函数,告知我给出的数字是否为素数,目前为止我的代码是:

def enter_input():
    n=input("Enter the number to be checked\n")
    return n

def check():
    o=int(enter_input())
    r=o
    print(type(o))
    print(type(r))
    bool=True
    for i in range(2,r,1):
        if(o%i==0):
            return False
    return True

def display():
    print("Entered Number Is {0}".format(p))
    print("The Number is a Prime:{0}".format(check()))


enter_input()
check()
display()

但是我在输入数字后得到了这个错误:

运行时:-

Enter the number to be checked
23
Traceback (most recent call last):
    File "Chech_Prime.py", line 8, in check
    o=int(enter_input())
    NameError: name 'enter_input' is not defined
回复关闭


我哪里出错了?这是我第一次尝试函数调用,我调用enter_input()函数的地方似乎找不到它。感谢您的帮助

您写了一些有点奇怪的东西,因此我将解释为什么这应该做您正在检查的事情:

这是您的代码,我将在两行之间进行注释:

def enter_input():
    n=input("Enter the number to be checked\n")
    return n 

def check(): # you create the check function, and then a display function, and is not necesary, because you want to do both thinks in the same function
    o=int(enter_input()) # you call this function a lot of times, and you only want to input the value once, so, is better to call the input method inside the function
    r=o # is better to name variables more descriptive, I mean, check() as a function, what checks? maybe you want to call it is_prime(), more descriptive, o and p... doesn't say anything, but input sounds better
    print(type(o))
    print(type(r)) #this auxiliar variable is not needed
    bool=True
    for i in range(2,r,1):
        if(o%i==0):
            return False
    return True

def display():
    print("Entered Number Is {0}".format(p))
    print("The Number is a Prime:{0}".format(check()))# here you again call the function, and again it ask to you to add a input... not good


enter_input()
check() # and again.. three times, that's so confusing even to follow when you are adding the inputs
display()
我的方法是:

def print_is_prime():
    value_to_eval=int(input("Enter the number to be checked\n"))

    print("the type of the value is " + str(value_to_eval.__class__.__name__))

    is_prime =True #initial value is correct
    for i in range(2,value_to_eval,1):
        if(value_to_eval%i==0):
            is_prime = False #here, you detect is not prime
            break # so with break you can avoid to execute innecesary all the loop

    is_prime = True and is_prime # with and operator, true is the neutral, so the value will be always the other value, in this case, is_prime, this like 0 + x, the value will be always x

    print("Entered Number Is {0}".format(value_to_eval))
    print("The Number is a Prime:{0}".format(is_prime))

print_is_prime()

你写了一些有点奇怪的东西,所以我将解释为什么这应该做你正在观察的事情:

这是您的代码,我将在两行之间进行注释:

def enter_input():
    n=input("Enter the number to be checked\n")
    return n 

def check(): # you create the check function, and then a display function, and is not necesary, because you want to do both thinks in the same function
    o=int(enter_input()) # you call this function a lot of times, and you only want to input the value once, so, is better to call the input method inside the function
    r=o # is better to name variables more descriptive, I mean, check() as a function, what checks? maybe you want to call it is_prime(), more descriptive, o and p... doesn't say anything, but input sounds better
    print(type(o))
    print(type(r)) #this auxiliar variable is not needed
    bool=True
    for i in range(2,r,1):
        if(o%i==0):
            return False
    return True

def display():
    print("Entered Number Is {0}".format(p))
    print("The Number is a Prime:{0}".format(check()))# here you again call the function, and again it ask to you to add a input... not good


enter_input()
check() # and again.. three times, that's so confusing even to follow when you are adding the inputs
display()
我的方法是:

def print_is_prime():
    value_to_eval=int(input("Enter the number to be checked\n"))

    print("the type of the value is " + str(value_to_eval.__class__.__name__))

    is_prime =True #initial value is correct
    for i in range(2,value_to_eval,1):
        if(value_to_eval%i==0):
            is_prime = False #here, you detect is not prime
            break # so with break you can avoid to execute innecesary all the loop

    is_prime = True and is_prime # with and operator, true is the neutral, so the value will be always the other value, in this case, is_prime, this like 0 + x, the value will be always x

    print("Entered Number Is {0}".format(value_to_eval))
    print("The Number is a Prime:{0}".format(is_prime))

print_is_prime()

“p”变量是在哪里定义的?很抱歉是“p”,它应该是“o”。但是问题仍然存在。好吧,我修改了一点你的代码试试看,在中途我会给你写一个解释让我知道这是否对你有效,我会很快更新,与你的代码进行比较,告诉你哪里犯了小错误,为什么我做了我做的事我给你添加了解释为什么你的代码不起作用,还有我为什么做我的。再见!“p”变量是在哪里定义的?很抱歉是“p”,它应该是“o”。但是问题仍然存在。好吧,我修改了一点你的代码试试看,在中途我会给你写一个解释让我知道这是否对你有效,我会很快更新,与你的代码进行比较,告诉你哪里犯了小错误,为什么我做了我做的事我给你添加了解释为什么你的代码不起作用,还有我为什么做我的。再见!谢谢Damian,修改后的代码成功了。我试图学习函数以及它们相互调用的方式,所以我制作了3个不同的函数,而不是一个。我会检查新的代码并解决问题。@Ritesh\u BM我一直很乐意帮忙,别忘了接受(✓) 如果你喜欢,请投票:)谢谢Damian,修改后的代码起作用了。我试图学习函数以及它们相互调用的方式,所以我制作了3个不同的函数,而不是一个。我会检查新的代码并解决问题。@Ritesh\u BM我一直很乐意帮忙,别忘了接受(✓) 如果你愿意,可以投票:)