each time i type a value the code gives me "subscribe" ...how to get the dict value?
Friends = {
'rolf' : 'blue',
'ronnie' : 'green',
'barbara' : 'purple',
'benny' : 'kaki',
'stewart' : 'yellow',
'mickey' : 'red'
}
def greetagain():
friend = input("Enter your friend name or color? ")
i = Friends.values()
if friend in Friends.keys():
print('yes it matches')
elif i in Friends.values():
print('ok ok')
else:
print('subscribe')
greetagain()
welcome to SO. Please note the change below, you use the variable friend without the i
, since you can check with friend values and keys
def greetagain():
friend = input("Enter your friend name or color? ")
if friend in Friends.keys():
print('yes it matches')
elif friend in Friends.values():
print('ok ok')
else:
print('subscribe')
i could only understand a part of your question, so, i have tried my best to explain it.
The if condition already works as it is. I think you want to retrieve the name of the friend (key) in the elif condition, for that you can try the following code:
Friends = { 'rolf' : 'blue', 'ronnie' : 'green', 'barbara' : 'purple', 'benny' : 'kaki', 'stewart' : 'yellow', 'mickey' : 'red' }
def greetagain():
friend = input("Enter your friend name or color? ")
if friend in Friends.keys():
print('Key matches')
print("Color associted to friend is:", Friends[friend])
elif friend in Friends.values():
for key, value in Friends.items():
if friend == value:
print('Color is present in list and associated by:', key)
else:
print('subscribe')
greetagain()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.