git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How can i stop this Infinite While Loop - Python


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 .