Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2004, 06:30 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
Passing dictionary elements

I need to be able to pass dictionary elements into a function but it keeps telling me it is not the correct type.

Code:
my_function(my_dict['element'])


Code:
def my_function(dict_element):
var = "%s" % dict_element


This does not work. It won't convert the dict_element to a string. Somehow I need to pass those elements into the function. Any suggestions are appreciated.

Reply With Quote
  #2  
Old June 5th, 2004, 01:01 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I dont see any reason why this shouldnt work. The only problem i can see would be a KeyError if the element didn't exist within the dictionary. Does the dictionary contain strings or digits. In any case give this a go:

Code:
elements = {'one': 'two', 'three': 'four'}

def function(element):
    print element

function(elements['one'])


This should simply show the element in the console. Maybe you can show us your whole program since i can't see any reason to do 'var = "%s" % element'; if you do want to convert the element to a string and it isnt already then use the str() function instead i.e.

Code:
var = str(element)


Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old June 5th, 2004, 09:41 AM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
Thanks for the response. Unfortunately the program is at work and I fired off that question right before I left. I'll look it over again and if I can't figure it out repost on Monday.

Reply With Quote
  #4  
Old June 7th, 2004, 02:41 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
Stepped in Monday morning and got it all to work within 10 minutes. I think the problem was that it was 5:45pm on Friday and I wanted to get the hell out of here.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Passing dictionary elements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT