HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   changing type in python (http://www.howtoforge.com/forums/showthread.php?t=26864)

CMYK 16th September 2008 05:23

changing type in python
 
I think I might be going about this a strange way in Python, but here's what I've been doing.
Code:

#!/usr/bin/env python

entered = raw_input("Enter numbers, separate numbers by spaces ")
numbers = entered.split(" ")

This achieves what I want but trying to pass this through a for statement and do arithmetic seems to not work
Code:

...
temp = 0
for elem in numbers:
    temp = temp + elem

So I get this error: TypeError: unsupported operand type(s) for +: 'int' and 'str'

So I guess I'm asking can you make python think that elem in the for statement is an int and definitely not a string.

ghostdog74 20th September 2008 14:42

you can convert the str into int before the addition using int()


All times are GMT +2. The time now is 05:56.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.