遇到任务2的一些问题

时间:2014-11-28 12:00:01

标签: python python-2.7

         #Loop starts here
         # This one provides the option to select one of the three tasks.
 task = input (' Hello! Welcome to Patricia G. Myrons program select task 1, 2 or  3. \n')

         #If the user selects task 1, the program will perform the Simple Divisibility Test 
if task == 1  : 
  print "Task 1 is here! \n"
  print "I can tell you if n is evenly divisible by m \n"
  print "Enter the following"
  n = input("Integer:")
  m = input("Integer:")
  evaluation = n % m
if evaluation == 0:
  print n, "/", m, " evenly divides"
else:
   print n, "/", m, " Sorry, does not evenly divide. Try again!"



  #If the user selects task 2, the program will perform the Prime Test 
if task == 2 : 
   print "Task 2 is here! \n"
   print "I can tell you if the number you enter is prime or not  \n"

   number=int(raw_input("Enter a number "))
elif number <= 1:
   print "Sorry! It is not prime"
else:
   n=2
   check = True
while n != m:
    if m%n == 0:
        print "Yeas! The number you entered is prime"
        check = False
        break
    n+=1
if check == True:
        print "Yeas! The number you entered is prime" 



        #If the user selects task 3, the program will display a list of factor numbers 
if task == 3: 
        print "Task 3 is here! \n"
        print "I can tell you all of the factors of the number you enter \n"
def print_factors(n):

        print("The factors of",n,"are:")
        for i in range(1, n + 1):
  if n % i == 0:
       print(i)


   num = int(input("Enter any number: "))

我刚用Python完成了我的程序,但是我在使用任务2时遇到了一些问题。

1 个答案:

答案 0 :(得分:-1)

您的代码存在缩进问题。

此外,代码中使用的变量未使用默认值定义。

我在代码之上定义了所有变量的默认值。

请参阅以下代码。

evaluation = 0
n = 0
m = 0
number = 0
check = False

task = input (' Hello! Welcome to Patricia G. Myrons program select task 1, 2 or  3. \n')

         #If the user selects task 1, the program will perform the Simple Divisibility Test
if task == 1  :
    print "Task 1 is here! \n"
    print "I can tell you if n is evenly divisible by m \n"
    print "Enter the following"
    n = input("Integer:")
    m = input("Integer:")
    evaluation = n % m
    if evaluation == 0:
        print n, "/", m, " evenly divides"
    else:
        print n, "/", m, " Sorry, does not evenly divide. Try again!"



#If the user selects task 2, the program will perform the Prime Test
if task == 2 :
    print "Task 2 is here! \n"
    print "I can tell you if the number you enter is prime or not  \n"

    number=int(raw_input("Enter a number "))
    if number <= 1:
        print "Sorry! It is not prime"
    else:
        n=2
        check = True
    while n != m:
        if m%n == 0:
            print "Yeas! The number you entered is prime"
            check = False
            break
        n+=1
    if check == True:
        print "Yeas! The number you entered is prime"



#If the user selects task 3, the program will display a list of factor numbers
if task == 3:
    print "Task 3 is here! \n"
    print "I can tell you all of the factors of the number you enter \n"
    def print_factors(n):

        print("The factors of",n,"are:")
        for i in range(1, n + 1):
            if n % i == 0:
                print(i)
    num = print_factors(int(input("Enter any number: ")))