How can i stop this Infinite While Loop - Python

ferzan saglam ferzans97 at gmail.com
Wed Oct 30 12:38:29 EDT 2019


On Wednesday, October 30, 2019 at 2:19:32 PM UTC, Matheus Saraiva wrote:
> m 30/10/2019 08:55, ferzan saglam escreveu:
> > total = 0
> > while True:
> >
> >    print('Cost of item')
> >    item = input()
> >
> >    if item != -1:
> >      total += int(item)
> >    else:
> >      break
> >
> > print(total)
> The program does not stop because its code does not contain any deals 
> that make the loop stop after 10 rounds. You can simply implement a 
> counter that counts the number of times the loop has passed.
> 
> You also did not convert item to int to make the comparison.
> 
> total = 0
> rounds = 0
> while rounds <= 10:
> 
>    print('Cost of item')
>    item = input()
> 
>    if int(item) > -1:
>      total += int(item)
>      rounds += 1
>    else:
>      break
> 
> print(total)



Thanks, it Works superbly. 
To get the limit of 10 i wanted, i had to make a slight change: 
while rounds <= 9 .


More information about the Python-list mailing list