[Tutor] Loops

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Apr 5 15:11:36 CEST 2015


On 05/04/2015 12:11, Marcus Lütolf wrote:
> Why do I get this traceback with the infinite loop below but not with the
> definitw loop (bottom of mail)?
> Thanks for help, Marcus.
>
> count = 0
> total = 0
> while True:
>
>      x = raw_input('Enter a number')
>      count = count + 1
>      total = total + x
>      print count, total, (total/count)
>
> Traceback (most recent call last):
>
>    File "C:\Python27\enter_a_number.py", line 6, in <module>
>
>      total = total + x
>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>

raw_input returns a string, you must convert that to an int (or some 
other numeric type) before you can add it to your total.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list