Intro to Python: How to ask the user if they want to repeat the for loop?

Question:

I need help figuring out how to ask the user if they would like to repeat the program. Any help is much appreciated. I am relatively new to Python and it would be great to receive some advice!

X = int(input("How many numbers would you like to enter? "))

Sum = 0
sumNeg = 0
sumPos = 0
for i in range(0,X,1):
    number = float(input("Please enter number %i : " %(i+1) ))

    # add every number to Sum
    Sum = Sum + number # Sum += number

    #only add negative numbers to sumNeg
    if number < 0:
        sumNeg += number # sumNeg = sumNeg + number
    #only add positive numbers to sumPos
    if number > 0:
        sumPos += number # sumPos = sumPos + number

print ("------")

print ("The sum of all numbers = ", Sum)
print ("The sum of negative numbers = ", sumNeg)
print ("The sum of positive numbers = ", sumPos)

I’m not sure if I need to use a while loop, but how would I enter a (y/n) into my code because I am asking for an integer from the user in the beginning.

Asked By: OnePieceFan19

||

Answers:

Ask the user for input at the end of the loop to continue or not. If the user doesn’t want to continue, use a break statement to break out of the loop.

https://www.simplilearn.com/tutorials/python-tutorial/break-in-python#:~:text=’Break’%20in%20Python%20is%20a,condition%20triggers%20the%20loop’s%20termination.

Answered By: Ashwin M

Have an outer loop that comprises your whole processing.
It’s an infinite loop.
You exit from this loop thanks to the break statement once the user has answered no.
In fact once he has answered everything else than Y or y. The user string is converted to lower() before comparison for more convenience.

while True:
    X = int(input("How many numbers would you like to enter? "))
    Sum = 0
    sumNeg = 0
    sumPos = 0
    for i in range(0, X, 1):
        number = float(input("Please enter number %i : " % (i + 1)))

        # add every number to Sum
        Sum = Sum + number  # Sum += number

        # only add negative numbers to sumNeg
        if number < 0:
            sumNeg += number  # sumNeg = sumNeg + number
        # only add positive numbers to sumPos
        if number > 0:
            sumPos += number  # sumPos = sumPos + number

        print("------")

        print("The sum of all numbers = ", Sum)
        print("The sum of negative numbers = ", sumNeg)
        print("The sum of positive numbers = ", sumPos)

    resp = input("Would you like to repeat the program  ? (y/n)")
    if resp.lower() != "y":
        break

As an alternative to the while True loop.
You could declare at the top of the script a variable user_wants_to_play = True.
The while statement becomes while user_wants_to_play.
And set the variable to False when the user answers no.

Answered By: 0x0fba

The following program will keep on executing until user enter 0 to break the loop.

while True: 
    X = int(input("How many numbers would you like to enter? "))
    Sum, sumNeg, sumPos = 0
    for i in range(X):
        number = float(input("Please enter number %i : " %(i+1) ))
        Sum += number
        if number < 0:
            sumNeg += number
        if number > 0:
            sumPos += number
    print ("------")
    
    print ("The sum of all numbers = ", Sum)
    print ("The sum of negative numbers = ", sumNeg)
    print ("The sum of positive numbers = ", sumPos)
        
    print ("------")
    trigger = int(input("The close the program enter 0: "))
        
    if trigger == 0:
        break
Answered By: Muhammad Wasif Ijaz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.