Home > Software engineering >  How to put a choice to try again to repeat the loop?
How to put a choice to try again to repeat the loop?

Time:12-05

So after finishing the code, I would like to have an option where the user would like to try again by typing Y/n or N/n. How do I make it?

a=int(input('enter value of n: '))
i = 1
sum=0
   


while a < 1 or a > 300: 
    print(a, 'is not in the range 1-300') 
    exit()

for a in range(1,a 1):
     print (a, end = '  ') 
while i<=a:
    if i%3==0 or i%5==0:
      sum=sum i
    i=i 1
print('\nsum of all multiples of 3 and 5 is:',sum)

repeat=str(input('Would you like to try again? Y/N?'))


           
          



   

    
  


  

CodePudding user response:

Here's a simple way to do it (keeping your code structure) without any functions or jargon (for beginners :]):

from sys import exit # Just something to exit your code


repeat = 'y'    # So it starts the first time
while repeat.lower() == 'y':    # repeat.lower() -> convert the string 'repeat' to lowercase, incase user inputs 'Y'. you could also use the or keyword here though
    n = int(input("Enter value of n:\n>>> "))

    if n < 1 or n > 300:
        print("'n' must be between 1 - 300, not "   n) # You could use f-strings, check them out!
        exit()

    sum_of_multiples = 0

    # I'm combining your for and while loop as shown below
    for i in range(1, n   1):   # Don't name your 'i' and 'n' variables the same -> you did so with 'a'

        print(i, end=', ')  # There are cleaner ways to do this but I'm assuming this is what you want

        if i % 3 == 0 or i % 5 == 0:
            sum_of_multiples  = i


    # Printing output
    print(f"\nSum of all multiples of 3 and 5 is: {sum_of_multiples}")  # This is called an f-string

    repeat = input("\nDo you want to go again?\n>>> ")  # If you don't input Y/y, the outer while loop will break and terminate


print("Thank you for running me!")  # Prints after the program finishes running

Note that you don't need the exit() when checking if n is within 1 - 300, you could also just use break.

EDIT (WITH BREAK) Program without from sys import exit below:

repeat = 'y'    # So it starts the first time

while repeat.lower() == 'y':    # repeat.lower() -> convert the string 'repeat' to lowercase, incase user inputs 'Y'. you could also use the or keyword here though
    n = int(input("Enter value of n:\n>>> "))

    if n < 1 or n > 300:
        print("'n' must be between 1 - 300, not "   n) # You could use f-strings, check them out!
        break # This will mean that "Thank you for running me!" gets printed even if your input is wrong

    sum_of_multiples = 0

    # I'm combining your for and while loop as shown below
    for i in range(1, n   1):   # Don't name your 'i' and 'n' variables the same -> you did so with 'a'

        print(i, end=', ')  # There are cleaner ways to do this but I'm assuming this is what you want

        if i % 3 == 0 or i % 5 == 0:
            sum_of_multiples  = i


    # Printing output
    print(f"\nSum of all multiples of 3 and 5 is: {sum_of_multiples}")  # This is called an f-string

    repeat = input("\nDo you want to go again?\n>>> ")  # If you don't input Y/y, the outer while loop will break and terminate


print("Thank you for running me!")  # Prints after the program finishes running

Hope this helped!

  • Related