TLDR; declare if homework; doing someone's homework doesn't really help;
Python is not ALGOL/Pascal/C/C++ by any other name; Python assignments
should promote learning semantics as well as syntax; sometimes
re-stating the problem leads to an alternate/better solution.
On 31/10/19 12:55 AM, fe
ferzan saglam wrote:
> On Wednesday, October 30, 2019 at 2:19:32 PM UTC, Matheus Saraiva wrote:
>> rounds = 0
>> while rounds <= 10:
...
> Thanks, it Works superbly.
> To get the limit of 10 i wanted, i had to make a slight change:
> while rounds <= 9 .
That's the (in)famous "off by one"
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
> On Oct 30, 2019, at 4:55 AM, ferzan saglam wrote:
>
> 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:
> item = input()
> if item != -1:
If you try this in the REPL, you'll see that 'item' is a string. You're
trying to compare it to an integer, which will always fail.
The cheapest way to fix this is probably:
if item != '-1':
Best,
G
--
https://
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
>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 compa