-1

So all I want to create is a loop for this program so that it will be able to be used more than once without re-running through the shell, I would like to also learn how to do this with any program involving functions

Everything

    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!')

I just want it to work when you press y and re-run the entire program like its first time

c h
  • 1
  • 1

1 Answers1

0

Structure your code. Create functions. Call functions. Respect indentations.

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()
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69