首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我试图让这个程序在使用所有函数的同时重复执行,但似乎无法使其正常工作

我试图让这个程序在使用所有函数的同时重复执行,但似乎无法使其正常工作
EN

Stack Overflow用户
提问于 2019-06-06 02:49:05
回答 1查看 12关注 0票数 -1

所以我想为这个程序创建一个循环,这样它就可以多次使用,而不需要通过shell重新运行,我还想学习如何在任何涉及函数的程序中做到这一点

所有的一切

    print('Welcome to the prime checker!')
   num = int(input("Enter any number between 1 - 5000: "))

      def is_prime(num):

if num > 1:
    for i in range(2, num):
        if (num % i) == 0:
            print(num, "is not prime")
            break
      else:
            print(num, "is prime")
      else:
       print(num, "is not a prime number")
   is_prime(num)

  def print_factors(num):


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


 print_factors(num)

 def main():
  choice = "y"
  while choice.lower() == "y":
    # get input
    #num = int(input("Enter any number between 1 - 5000: "))


    # see if the user wants to continue
    choice = input("Repeat? (y/n): ")
    print()

print("Bye!")

  if __name__ == "__main__":
          main()



  print('Finished!')

我只希望当你按下y键并像第一次一样重新运行整个程序时,它就能正常工作。

EN

回答 1

Stack Overflow用户

发布于 2019-06-06 03:22:14

组织你的代码。创建函数。调用函数。尊重缩进。

def check_primes():
    print('Welcome to the prime checker!')
    num = int(input("Enter any number between 1 - 5000: "))

    def is_prime(num): 
         # do checking -your current code makes not much sense
         # and does not do what the methodnames imply
         # see f.e. https://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime
         print("Did prime checking")

def main():
    choice = "y"
    while choice.lower() == "y":
        check_primes()

        # see if the user wants to continue
        choice = input("Repeat? (y/n): ")

if __name__ == "__main__":
    main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56466370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档