Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading