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:05 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 12:43 PM
Try this:

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



Or you may prefer this one:

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

Report
Re: Python Dictionaries Posted by em-arcamenel on 9 Sept 2012 at 12:47 PM
Try this:

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



Or you may prefer this one:

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




 

Recent Jobs