(New to Python) Shopping List Code
Bev In TX
countryone77 at gmail.com
Mon Oct 28 10:13:22 EDT 2019
> On Oct 28, 2019, at 8:42 AM, ferzan saglam <ferzans97 at gmail.com> wrote:
>
> How can I stop this code when -1 is typed or at a maximum item count of ten.
> At the moment the code seems to be in a infinite loop meaning it keeps on asking for an entry until -1 is typed
>
>
> total = 0
> while True:
>
> print('Cost of item')
>
> item = input()
>
> if item != -1:
> total = total + item
> if item == -1:
>
> break
>
> print(total)
> --
Sorry about the blank post— I attempted to scroll down, but instead of doing that,it sent the message.
From the documentation (emphasis is mine):
“input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
So you don’t have a number; you have a string. Presuming that the string contains an integer, You can convert that string to an integer number with the built in int() function.
Bev in TX
More information about the Python-list
mailing list