简体   繁体   English

访问字典中的部分值

[英]Accessing parts of values in a dictionary

Got an assignment to write a program that performs actions on a given dictionary according to an integer of a user input根据用户输入的 integer 得到一个任务来编写一个对给定字典执行操作的程序

Celebrity = {"first_name": "Mariah", "last_name": "Carey", "birth_date": "27.03.1970", "hobbies": ["sing", "compose", "act"]}

If the user input is 2, the program is supposed to print Maria's birth month ("3" - part of the string).如果用户输入是 2,则程序应该打印 Maria 的出生月份(“3” - 字符串的一部分)。 If the user input is 4, the program is supposed to print the last hobby on the list ("act").如果用户输入是 4,则程序应该打印列表中的最后一个爱好(“act”)。

I tried:我试过了:

user_input = input ("Please enter a number between 1 and 8: ")

if int(user_input) == 2:
    print(Celebrity["birth_date"[4:5])

if int(user_input) == 4:
    print (Celebrity["hobbies"[2]])

Both of these conditions end up giving me KeyErrors, how do I go about accessing only a part of a value?这两个条件最终都给了我 KeyErrors,我如何 go 关于仅访问值的一部分?

Your syntax is Celebrity["birthdate"[4:5]) which create error.您的语法是Celebrity["birthdate"[4:5])会产生错误。 change it with Celebrity["birth_date"][4:5] .Celebrity["birth_date"][4:5]改变它。 and Celebrity['hobbies[2]'] also create error change it with Celebrity["hobbies"][2] .Celebrity['hobbies[2]']也会创建错误,用Celebrity["hobbies"][2]更改它。

Try this:尝试这个:

Celebrity = {"first_name": "Mariah", "last_name": "Carey", "birth_date": "27.03.1970", "hobbies": ["sing", "compose", "act"]}
user_input = input ("Please enter a number between 1 and 8: ")

if int(user_input) == 2:
    print(Celebrity["birth_date"][4:5])

if int(user_input) == 4:
    print (Celebrity["hobbies"][2])

Output: Output:

Please enter a number between 1 and 8: 2
3

Please enter a number between 1 and 8: 4
act

When you want to get value you need to use key in square brackets.当您想获得价值时,您需要在方括号中使用键。 If you want get only mount you can get value by index.如果您只想获取挂载,则可以通过索引获取值。 And if you want get last value in array you can use index -1 like this Celebrity["hobbies"][-1]如果你想得到数组中的最后一个值,你可以使用 index -1像这样Celebrity["hobbies"][-1]

Celebrity = {"first_name": "Mariah", "last_name": "Carey", "birth_date": "27.03.1970", "hobbies": ["sing", "compose", "act"]}

user_input = input ("Please enter a number between 1 and 8: ")

if int(user_input) == 2:
    print(Celebrity["birth_date"][4])

if int(user_input) == 4:
    print (Celebrity["hobbies"][-1])

Here you go;你在这里 go;

Celebrity = {"first_name": "Mariah", "last_name": "Carey", "birth_date": "27.03.1970", "hobbies": ["sing", "compose", "act"]}


user_input = input ("Please enter a number between 1 and 8: ")


if int(user_input) == 2:
print(Celebrity["birth_date"][4:5])

if int(user_input) == 4:
print (Celebrity["hobbies"][2])

You were placing brackets on the wrong spots, which were then getting "h" and "b" from "birth_date" and "hobbies" as keys.您将括号放在错误的位置,然后将“birth_date”和“hobbies”中的“h”和“b”作为键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM