[英]python dict values in elif loop
each time i type a value the code gives me "subscribe" ...how to get the dict value?每次我输入一个值时,代码都会给我“订阅”...如何获取 dict 值?
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.欢迎来到 SO。 Please note the change below, you use the variable friend without the
i
, since you can check with friend values and keys请注意下面的更改,您使用不带
i
的变量朋友,因为您可以检查朋友的值和键
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. if 条件已经可以正常工作了。 I think you want to retrieve the name of the friend (key) in the elif condition, for that you can try the following code:
我认为您想在 elif 条件中检索朋友(键)的名称,为此您可以尝试以下代码:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.