[英]How do I append an input string into a dictionary key int?
I'm trying to make the following code prompt for an answer and than add that answer into the dictionary titled poll. 我正在尝试为答案做出以下代码提示,然后将该答案添加到名为poll的字典中。 I'm trying the append command which is usually used for list but it doesn't just work for a dictionary.
我正在尝试通常用于列表的append命令,但它不仅适用于字典。
At the moment I neither managed to update the key values (my intended goal) or even managed to just add a new item to the dictionary (non intended). 目前我既没有设法更新键值(我的预期目标),也没有设法只添加一个新项目到词典(非预期)。
poll = {
'cat': 0,
'dog': 0,
'both': 0,
'none': 0,
}
prompt = "Are you a cat owner, dog owner, both, or none? "
prompt += "\nEnter 'quit' to stop this loop \n"
message = ""
while message != "quit":
message = input(prompt)
poll[message].append(value)
if message != "quit":
print("Your answer was submitted! \n")
print("Poll results:\n", poll)
If you want to add a new key-value pair to the dictionary, instead of appending you can do the following, although I do not see a variable value
in your code. 如果要向字典添加新的键值对,可以执行以下操作,而不是追加,但我在代码中看不到变量
value
。 It will also update the value of existing key if entered again because dictionary cannot have duplicate keys 如果再次输入,它还将更新现有密钥的值,因为字典不能有重复的密钥
poll[message] = value
Assuming you take care of the variable value
, the code will look like 假设您处理变量
value
,代码将如下所示
As pointed out by David Buck, you need to move the poll[message] = value
part inside the if
statement 正如David Buck所指出的,你需要在
if
语句中移动poll[message] = value
部分
while message != "quit":
message = input(prompt)
if message != "quit":
poll[message] = value
print("Your answer was submitted! \n")
print("Poll results:\n", poll)
I think you may be mistaking something. 我想你可能会误会。 You don't have any list nor a variable called
value
. 您没有任何列表,也没有名为
value
的变量。 If you are trying to count the number of people with pets you may use poll[message] += 1
. 如果您正在尝试计算携带宠物的人数,您可以使用
poll[message] += 1
。
Eg: 例如:
poll = {
'cat': 0,
'dog': 0,
'both': 0,
'none': 0,
}
poll['cat'] += 1 # poll['cat'] = poll['cat'] + 1
poll['cat'] += 1 # poll['cat'] = poll['cat'] + 1
poll['dog'] += 1 # poll['dog'] = poll['dog'] + 1
print(poll)
# {'cat': 2, 'dog': 1, 'both': 0, 'none': 0}
In addition, you should change the condition of the while
and the order of the if
statement: 此外,您应该更改
while
的条件和if
语句的顺序:
poll = {
'cat': 0,
'dog': 0,
'both': 0,
'none': 0,
}
prompt = "Are you a cat owner, dog owner, both, or none? "
prompt += "\nEnter 'quit' to stop this loop \n"
message = ""
while True:
message = input(prompt)
if message != "quit":
print("Your answer was submitted! \n")
else:
break
poll[message] += 1
print("Poll results:\n", poll)
Test: 测试:
Are you a cat owner, dog owner, both, or none?
Enter 'quit' to stop this loop
>>> cat
Your answer was submitted!
Are you a cat owner, dog owner, both, or none?
Enter 'quit' to stop this loop
>>> cat
Your answer was submitted!
Are you a cat owner, dog owner, both, or none?
Enter 'quit' to stop this loop
>>> dog
Your answer was submitted!
Are you a cat owner, dog owner, both, or none?
Enter 'quit' to stop this loop
>>> quit
Poll results:
{'cat': 2, 'dog': 1, 'both': 0, 'none': 0}
Obviously, there are a lot more things you can do, like using poll.get(key, default)
to add values, e.: poll[message] = poll.get(message, 0) + 1
, so even if the key doesn't exist in the dictionary it doesn't crash the script, instead, it makes a new key. 显然,你可以做很多事情,比如使用
poll.get(key, default)
来添加值,例如: poll[message] = poll.get(message, 0) + 1
,所以即使密钥也是如此字典中不存在它不会使脚本崩溃,相反,它会生成一个新密钥。 Or before adding the value to the dictionary check if message in poll.keys(): poll[message] += 1
so the script only accepts cat
, dog
, both
or none
but not car
or bird
. 或者在将值添加到字典之前检查
if message in poll.keys(): poll[message] += 1
因此脚本只接受cat
, dog
, both
或none
但不接受car
或bird
。
I would just update the value of the dictionary based on the vote. 我只会根据投票更新字典的值。 Here's an example for me entering 'dog':
这是我进入'狗'的一个例子:
Initial Dictionary: 初始词典:
poll = {
'cat': 0,
'dog': 0,
'both': 0,
'none': 0,
}
My response: 我的回复:
my_response = 'dog'
Update the 'dog' key: 更新'dog'键:
poll[my_response] += 1
Output: 输出:
{'cat': 0, 'dog': 1, 'both': 0, 'none': 0}
this need to work: 这需要工作:
poll = {
'cat': 0,
'dog': 0,
'both': 0,
'none': 0,
}
prompt = "Are you a cat owner, dog owner, both, or none? "
prompt += "\nEnter 'quit' to stop this loop \n"
message = ""
while message != "quit":
message = input(prompt)
if message in list(poll):
pass
else:
#else it will give a error that it isn't a key
continue
num = poll[message]
num += 1
poll[message] = num
if message != "quit":
print("Your answer was submitted! \n")
print("Poll results:\n", poll)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.