简体   繁体   English

如何将输入字符串附加到字典键int?

[英]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因此脚本只接受catdogbothnone但不接受carbird

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.

相关问题 我如何将字典 append 作为现有字典键的另一个值 - How do I append a dictionary as another value to an existing dictionary key 如何将多个列表附加到python字典中的一个键? - How do I append multiple lists to one key in a python dictionary? 如何在python字典中附加键的值? - How do I append the value of a key in a python dictionary? 如果输入字符串与键匹配,如何打印字典的元素? - How can i print the elements of a dictionary if an input string matches with a key? 当键是字符串的第二个值时,如何将多个字符串列表附加到字典中的键? - How can I append multiple lists of strings to a key in dictionary when the key is the second value of a string? 如何在Python3中将字典追加到另一个字典字符串键? - How to append a dictionary to another dictionary string key in Python3? 如何在python中用字符串附加用户输入(整数)? - How do I append the user input (integer) with a string in python? 无法将字符串附加到字典键 - Cannot append string to dictionary key 我有一本字典 {string and int}。 如何比较整数值而不是同一字典中的键(python) - I have a dictionary {string and int}. How do I compare integers values and not the keys within the same dictionary(python) 如何将字符串添加到字典的每个键? - How do I add a string to each key of a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM