Home > Blockchain >  While loops in python while condition being at the end
While loops in python while condition being at the end

Time:10-31

I’m trying to write a program that has while true: at the start and then at the end asks the user if they want to repeat.

While True:
 print(“I will list the prime numbers between 0 and N”)
 N=input(“up to what number will I list the prime numbers?”)

 print (“prime numbers between 0 and”,N,” are:”)
 for no in range (2,N)
   if no>1:
       prime=True 
   for i in range (2, no):
      if (no % i) == 0:
          prime=False
          break
   if prime: 
       print(no)
 print(“would you like to enter a new range?”)
 response= input(“Enter yes or press enter key to exit”)

if response == yes:
         True

But no matter what is entered it keeps repeating

I tried adding an else: break

But it would say break away out of the loop of I’m not sure what to do

CodePudding user response:

Here's a fixed version, with fixes commented:

while True:  # needs to be 'while', not 'While'!
    print("I will list the prime numbers between 0 and N")  # use regular "", not “”
    N = int(input("up to what number will I list the prime numbers?"))  # make it an int!

    print("prime numbers between 0 and", N, "are:")
    for no in range(2, N):  # need a : to start the for loop
        # no starts at 2, so it will always be >1.  No "if" needed.
        prime = True
        for i in range(2, no):
            if no % i == 0:
                prime = False
                break
        if prime: 
            print(no)
    print("would you like to enter a new range?")
    response = input("Enter yes or press enter key to exit")
    if response != "yes":  # need quotes around "yes" to make it a string
        break
    # loop automatically continues if not broken

The part that's directly pertinent to your original question is at the very end -- what you want to do is break the loop if it's time to stop the program (i.e. if they didn't enter "yes"). If not broken, the loop will continue automatically since it's in a while True:.

This part:

        prime = True
        for i in range(2, no):
            if no % i == 0:
                prime = False
                break
        if prime: 
            print(no)

can also be written more simply as:

        for i in range(2, no):
            if no % i == 0:
                break
        else: 
            print(no)

The else: block after a for: is executed at the end of the loop, but only if it's not stopped early with a break; you can therefore use it for cases like this where you want to test a bunch of conditions, stop the loop if one of them is true, and do something else otherwise.

This is such a common use case that there's an even simpler way to do it, using the built-in all function:

        if all(no % i for i in range(2, no)):
            print(no)

Here the all function tests that all elements of the iterable are true (note that no % i is considered "truthy" as long as it doesn't equal zero), and returns True or False accordingly.

CodePudding user response:

Instead of using True as the while condition, use a variable as the condition such as:

while run

If you use True as the while condition True will always be true, so you have to use a variable in order for it to change.

Updated Code

run = True
While run:
 print(“I will list the prime numbers between 0 and N”)
 N=input(“up to what number will I list the prime numbers?”)

 print (“prime numbers between 0 and”,N,” are:”)
 for no in range (2,N)
   if no>1:
       prime=True 
   for i in range (2, no):
      if (no % i) == 0:
          prime=False
          break
   if prime: 
       print(no)
 print(“would you like to enter a new range?”)
 response= input(“Enter yes or press enter key to exit”)

if response == yes:
         run = True
else
         run = False
  • Related