Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Python Dictionaries Posted by n1ck3 on 13 Aug 2012 at 5:04 AM
I am writing a small application for a freind of mine, and it includes a dictionary. I need to have the user input the 'key' value and have the program output the 'value' value. Any ideas on how to do this?
P.S. - A way to inform the user that a key is not in the dictionary would also be nice.
Report
Re: Python Dictionaries Posted by em-arcamenel on 9 Sept 2012 at 9:46 PM
Try this:

myDict = {}  # Create your own
userKey = input()
if userKey in myDict:
    myValue = myDict[userKey]
    print('Value found: {}'.format(myValue))
else:
    print('No such key!')


Or you may prefer this one:

myDict = {}
userKey = input()
print(myDict.get(userKey, 'No such key!'))




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.