Python Prime编号程序错误

时间:2014-08-02 18:33:02

标签: python

我正在努力将一个能够执行与素数相关的不同任务的程序作为挑战。其中一项任务是说你输入的数字是素数(这是菜单中选项A定义为选项)。不循环的循环在定义为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()

2 个答案:

答案 0 :(得分:1)

首先,你从不打电话给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()

答案 1 :(得分:0)

在此代码中永远不会调用

nomtyre。所以其中的任何代码都永远不会执行。为了获得执行功能,你必须像这样调用它。

nomtyre()
相关问题