0

So the code before behaved properly before my "while type(number) is not int:" loop, but now when the user presses 0, instead of generating the sum of the list, it just keeps looping.

Would really appreciate some help with this! Thank you!

List = []



pro = 1
while(pro is not 0):
    number = False
    while type(number) is not int:
        try:
            number = int(input("Please enter a number: "))
            List.append(number)

        except ValueError:

            print("Please only enter integer values.")


    if(number == 0):
        Sum = 0
        for i in List:
            Sum = i + Sum
        ans = 0

print(Sum)
Catz
  • 11
  • 5
  • 1
    `while type(number) is not int:` cannot be `False` since `number` is an integer. – Jean-François Fabre Nov 18 '17 at 17:47
  • Yeah, i had it as an int before i posted and when I do that the program just doesn't run at all:/. – Catz Nov 18 '17 at 17:49
  • What are `pro` and `ans` ? Shouldn't they be the same variable? – Naïm Favier Nov 18 '17 at 17:50
  • Where do you change the value of `pro`? If you do not change it, loop will continue forever. – John Anderson Nov 18 '17 at 17:50
  • Uhg thanks, those are artifacts from everything I have tried, i forgot to delete ans. Pro, is just how i am trying to keep the loop going, until the user enters 0. Then I want to print the sum of the list. I had it all working before the inner while loop, but it would crash if a user entered anything but an int. – Catz Nov 18 '17 at 17:52
  • Have you looked at any of the Q&A on the same subject? Like here: https://stackoverflow.com/questions/19984168/python-checking-whether-or-not-a-variable-is-a-int-using-while-loop – trincot Nov 18 '17 at 17:53
  • When I changed number = 1 instead of false, the program doesn't do anything at all. – Catz Nov 18 '17 at 17:53
  • You should use [isinstance()](https://docs.python.org/3/library/functions.html#isinstance) in your condition. – wwii Nov 18 '17 at 17:59

4 Answers4

0

Put if number == 0: inside while type(number) is not int: loop like this:

List = []

while True:
    try:
        number = int(input("Please enter a number: "))

        if number == 0:
            Sum = 0
            for i in List:
                Sum = i + Sum

            print(Sum)
            break

        List.append(number)

    except ValueError:
        print("Please only enter integer values.")
Elis Byberi
  • 1,422
  • 1
  • 11
  • 20
0

Actually, this should keep looping forever for all numbers the user may input, not just zero.

To fix this, you can just add this break condition after (or before, it doesnt really matter) appending:

number = int(input("Please enter a number: "))
List.append(number)
if number == 0:
   break
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

I'm guessing that you want to end while loop when user inputs 0.

List = []

pro = 1
while pro is not 0:
    try:
        number = int(input("Please enter a number: "))
        List.append(number)
        # This breaks while loop when number == 0
        pro = number
    except ValueError:
        print("Please only enter integer values.")

Sum = 0
for i in List:
    Sum += i

print(Sum)

EDIT: I have also cleaned the unnecessary code.

Borut
  • 3,304
  • 2
  • 12
  • 18
0

So I got it to work, when written like this:

List = []



pro = 1
while(pro is not 0):
    while True:
        try:
            number = int(input("Please enter a number: "))
            List.append(number)
            break
        except ValueError:

            print("Please only enter integer values.")


    if(number == 0):
        Sum = 0
        for i in List:
            Sum = i + Sum
        pro = 0

print(Sum)

But I don't really understand how this is making it only take int values, any clarification would be really helpful, and otherwise thank you all for your help!

Catz
  • 11
  • 5
  • When the user types a non-integer the call `int(input(...))` will throw a ValueError exception and, so the code will jump to the `except ValueError:` clause and execute the print statement there. The `while(True):` loop will then continue. – John Anderson Nov 19 '17 at 01:02