git.net

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

How can i stop this Infinite While Loop - Python


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)