#1  
Old 16th September 2008, 06:23
CMYK CMYK is offline
Junior Member
 
Join Date: Sep 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote
Sponsored Links
  #2  
Old 20th September 2008, 15:42
ghostdog74 ghostdog74 is offline
Junior Member
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
 
Default

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

Bookmarks

Tags
python, settype

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
chgrp error new user DKA General 15 28th July 2008 18:45
named.conf.master ISPconfig duplication hypertec_hope Installation/Configuration 0 18th July 2008 19:23
2.4.17 system needed modul8r Installation/Configuration 16 31st May 2007 15:15
Cannot create client, reseller and hosting plan crapufish Installation/Configuration 22 15th March 2007 11:27
view webalizer stats kuyaedz General 22 12th March 2006 15:45


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


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