Python素数程序错误

2024-10-03 11:25:42 发布

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

我试图做一个挑战程序,可以执行不同的任务有关素数。其中一项任务是说明您输入的数字是否为素数(这是菜单中定义为选项的选项)。不循环的循环位于定义为nomtyre的节中。如果你能看一眼,告诉我你认为它有什么问题,那对我真的很有帮助。你知道吗

    import time

def nomtyre():
    divider = 2
    if divider == number:
        print (number," is a prime number.")
        divider = 2
        time.sleep(3)
        choice()
    else:
        if number % divider == 0:
            divider = divider + 1
            nomtyre()
        else:
            print (number," is not a prime number.")
            divider = 2
            time.sleep(3)
            choice()

def nomty():
    number = int(input("Please enter your whole number: "))

def choice():
    print ("Would you like to:")
    print ("a) Type in a number to be decided wheather it is a prime number or not.")
    print ("b) Have prime numbers calculated from 2 upwards.")
    print ("c) Exit.")
    answer = input("So what would you like to do? a/b/c: ")
    if answer == "a" or answer =="A":
        nomty()
    elif answer == "b" or answer == "B":
        nomup()
    elif answer == "c" or answer == "C":
        print ("Thank you for using Prime Number calcultor...")
        time.sleep(1.5)
    else:
        print ("Sorry, that wasn't a choice, please try again")
        time.sleep(1.5)
        choice()

print ("Welcome to Prime Number Calculator...")
time.sleep(1)
choice()

Tags: ortoanswernumberiftimeisdef
2条回答

nomtyre在该代码中从未被调用。所以里面的任何代码都不会执行。为了得到要执行的函数,必须这样调用它。你知道吗

nomtyre()

首先,你从不打电话给诺姆蒂尔,这就是为什么它从不跑。另外,nomtyre从不定义除法器或数字(我想您应该将它们作为参数传入)。尝试以下操作:

import time

def nomtyre(number, divider):
    if divider == number:
        print (number," is a prime number.")
        divider = 2
        time.sleep(3)
        choice()
    else:
        if number % divider == 0:
            divider = divider + 1
            nomtyre(number, divider)
        else:
            print (number," is not a prime number.")
            time.sleep(3)
            choice()

def nomty():
    return int(input("Please enter your whole number: "))

def choice():
    print ("Would you like to:")
    print ("a) Type in a number to be decided wheather it is a prime number or not.")
    print ("b) Have prime numbers calculated from 2 upwards.")
    print ("c) Exit.")
    answer = input("So what would you like to do? a/b/c: ")
    if answer == "a" or answer =="A":
        nomtyre(nomty(), 2) #makes the given input the number and sets the divider to 2 initially
    elif answer == "b" or answer == "B":
        nomup()
    elif answer == "c" or answer == "C":
        print ("Thank you for using Prime Number calcultor...")
        time.sleep(1.5)
    else:
        print ("Sorry, that wasn't a choice, please try again")
        time.sleep(1.5)
        choice()

print ("Welcome to Prime Number Calculator...")
time.sleep(1)
choice()

相关问题 更多 >