How can i stop this Infinite While Loop - Python

Eko palypse ekopalypse at gmail.com
Wed Oct 30 08:28:30 EDT 2019


>From what I understand you want to give the user the possibility to try 10
times or enter -1 to end the script, right?
If so, you need to check if item is -1 or total tries are already 10 and in
such a case break the loop.
No need for an else branch.
Note, input returns an str object but you compare against an integer  ->
item != -1
This   total += int(item)   should be raised by one as you don't know what
item actually is, it might be a text

Eren

Am Mi., 30. Okt. 2019 um 13:01 Uhr schrieb ferzan saglam <
ferzans97 at gmail.com>:

> I have tried many ways to stop the infinite loop but my program doesn't
> seem to stop after 10 items! It keeps going...!)
>
> ---------------------------
> total = 0
> while True:
>
>   print('Cost of item')
>   item = input()
>
>   if item != -1:
>     total += int(item)
>   else:
>     break
>
> print(total)
> ---------------------------
>
>
> The output I am aiming for is for the code to stop asking 'Cost of item'
> after 10 inputs or until -1 is typed, but the code repeats itself on an
> infinite loop. I cannot seem to stop the infinite loop.
>
> Thanks for any help in advance.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list