Python 在字典中查找键并打印值

Python 在字典中查找键并打印值,python,dictionary,Python,Dictionary,大家好,我被困在一个课堂作业上,现在不知道该去哪里,因为我的大学没有提供编程领域的导师,因为这是第一个学期,这已经提供了。任务是: 编写一个程序: 在有用的消息中打印出该代码的玩具名称,如“该代码的玩具是棒球” 当用户输入“退出”而不是玩具代码时,程序退出 下面是dict应该从中填充的文本文件示例 D1,暴龙 D2,阿帕塔索罗斯 D3,Velociraptor D4,三角帽 D5,翼手龙 T1,柴油发电 T2,蒸汽机 T3车厢 到目前为止,我得到的是: ** ** 老实说,我甚至不确定我所拥有的

大家好,我被困在一个课堂作业上,现在不知道该去哪里,因为我的大学没有提供编程领域的导师,因为这是第一个学期,这已经提供了。任务是:

编写一个程序:

  • 在有用的消息中打印出该代码的玩具名称,如“该代码的玩具是棒球”
  • 当用户输入“退出”而不是玩具代码时,程序退出
  • 下面是dict应该从中填充的文本文件示例

    D1,暴龙

    D2,阿帕塔索罗斯

    D3,Velociraptor

    D4,三角帽

    D5,翼手龙

    T1,柴油发电

    T2,蒸汽机

    T3车厢

    到目前为止,我得到的是:

    **

    **

    老实说,我甚至不确定我所拥有的一切是否正确。非常感谢您的帮助。

    有两个问题

  • 你的字典没有被填充;但是,您的问题中目前没有足够的信息来帮助解决该问题。需要知道文件的外观,等等
  • 您的查找循环不会显示匹配键的值。下面是解决方案
  • 尝试迭代
    对,如下所示:

    for code, toy in toylookup.items():
        if key == search_toy_code:
            print('The toy for that code ({}) is a {}'.format(code, toy))
        else:
            print("Toy code ({}) not found".format(code))
    
    请查看以下文档:

    项目()

    返回字典项((键、值)对的新视图


    您应该熟悉基本的python编程。为了解决这些任务,您需要了解基本的数据结构和循环

    # define path and name of file
    filepath = "test.txt"  # content like: B1,Baseball B2,Basketball B3,Football
    
    # read file data
    with open(filepath) as f:
        fdata = f.readlines()  # reads every line in fdata
                               # fdata is now a list containing each line
    
    # prompt the user
    print("please enter toy code here: ")
    user_toy_code = input()
    
    # dict container
    toys_dict = {}
    
    # get the items with toy codes and names
    for line in fdata:  # iterate over every line
        line = line.strip()  # remove extra whitespaces and stuff
        line = line.split(" ")  # splits "B1,Baseball B2,Basketball"
                                # to ["B1,Baseball", "B2,Basketball"]
        for item in line:  # iterate over the items of a line
            item = item.split(",")  # splits "B1,Baseball"
                                    # to ["B1", "Baseball"]
            toys_dict[item[0]] = item[1]  # saves {"B1": "Baseball"} to the dict
    
    # check if the user toy code is in our dict
    if user_toy_code in toys_dict:
        print("The toy for toy code {} is: {}".format(user_toy_code, toys_dict[user_toy_code]))
    else:
        print("Toy code {} not found".format(user_toy_code))
    

    我强烈建议你把这个问题分解成你遇到的一个具体问题。发布整个作业对人们来说有点让人望而生畏,让他们通读并回答!:)也许试着总结一下吧?提供一个“toys.txt”的示例片段和脚本所需的输出。也要尽量缩小问题的范围。如果不知道“toys.txt”是什么样子,那么评估代码是很困难的;)你有什么问题?我无法打印出任何返回值,你的
    toylookup
    是空的吗?我看不到在任何地方填充它……如果您填充从代码到玩具名的字典映射,您就不需要再迭代了。只需检查
    是否在toylookup中编码
    ,如果钥匙在那里,则使用
    toylookup[code]
    # define path and name of file
    filepath = "test.txt"  # content like: B1,Baseball B2,Basketball B3,Football
    
    # read file data
    with open(filepath) as f:
        fdata = f.readlines()  # reads every line in fdata
                               # fdata is now a list containing each line
    
    # prompt the user
    print("please enter toy code here: ")
    user_toy_code = input()
    
    # dict container
    toys_dict = {}
    
    # get the items with toy codes and names
    for line in fdata:  # iterate over every line
        line = line.strip()  # remove extra whitespaces and stuff
        line = line.split(" ")  # splits "B1,Baseball B2,Basketball"
                                # to ["B1,Baseball", "B2,Basketball"]
        for item in line:  # iterate over the items of a line
            item = item.split(",")  # splits "B1,Baseball"
                                    # to ["B1", "Baseball"]
            toys_dict[item[0]] = item[1]  # saves {"B1": "Baseball"} to the dict
    
    # check if the user toy code is in our dict
    if user_toy_code in toys_dict:
        print("The toy for toy code {} is: {}".format(user_toy_code, toys_dict[user_toy_code]))
    else:
        print("Toy code {} not found".format(user_toy_code))