简体   繁体   English

未定义function_name错误

[英]function_name is not defined error

I want to write a function in python that tells if the number I give is prime, my code so far is: 我想在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()

But I get this error after input the numer: 但输入数字后出现此错误:

RunTime:- 运行:-

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

Repl Closed 已关闭

Where did I go wrong? 我哪里做错了? This was my first try with function calling, It seems like the place i called the enter_input() function could not find it. 这是我第一次尝试使用函数调用,似乎我调用enter_input()函数的位置找不到它。 Thanks for any help 谢谢你的帮助

You write somethings a little odd, so I will explain why this should do what you are specting: 您写的内容有些奇怪,因此我将解释为什么这样做应该符合您的期望:

This is your code, I will comment between lines: 这是您的代码,我将在两行之间注释:

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()

My way to do it will be: 我的方法是:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM