简体   繁体   English

从文本字典访问值

[英]Accessing Values from Text Dictionary

I am trying to create a "This is Your New Name Generator" program. 我正在尝试创建一个“这是你的新名称生成器”程序。 I am doing this by asking the user for their first and last name. 我这样做是通过询问用户的名字和姓氏。 The program then takes the first letter of their first name, and the last letter of the their last name, and pulls from two text files to give their new first and last name. 程序然后获取他们的名字的第一个字母,以及他们姓氏的最后一个字母,并从两个文本文件中提取新的名字和姓氏。

I've gotten as far as getting the user's first and last name, and pulling information from a file, however it always gives me the last line of the file. 我已经获得了用户的名字和姓名,并从文件中提取信息,但它总是给我文件的最后一行。

I thought I could setup the files like dictionaries and then use the user's input as keys, but it doesn't seem to be working. 我以为我可以像字典一样设置文件,然后使用用户的输入作为键,但它似乎不起作用。

Any advice? 有什么建议?

firstName = input("What is your first Name? ")
lastName = input("What is your last Name? ")

fN = firstName[0].lower()
lN_len = len(lastName) -1
lN = lastName[lN_len]

fNdict = {} 
with open('firstName.txt') as f:
    for line in f:
        (fN, fNval) = line.split(",")
        fNdict[fN] = fNval

lNdict = {}
with open('lastName.txt') as fileobj:
    for line in fileobj:
        lNkey, lNvalue = line.split(",")
        lNdict[lN] = lNvalue
newFirstName = fNval
newLastName = lNvalue

print("Your zombie Name is: %s %s "%(newFirstName,newLastName))

Reference Image: 参考图片:

When you run these lines: 当您运行这些行时:

newFirstName = fNval
newLastName = lNvalue

fNval and lNvalue have the last values they had in their respective loops. fNvallNvalue具有它们各自循环中的最后一个值。 I think you mean to use the user's first and last names as keys to the dictionaries, eg 我认为你的意思是使用用户的名字和姓氏作为字典的键,例如

newFirstName = fNdict[fN]
newLastName = lNdict[lN]

Note that this will fail if fN and lN aren't in the dictionaries. 请注意,如果fNlN不在词典中,则会失败。 You might want to create defaultdict s instead. 您可能想要创建defaultdict

Note also that Python has an official style guide that most Python developers follow. 另请注意,Python有一个大多数Python开发人员遵循的官方样式指南 Please consider reading it and writing your code accordingly. 请考虑阅读并相应地编写代码。 The code you've shared is very hard to read. 您共享的代码非常难以阅读。

You could follow a slightly different implementation to achieve the same result. 您可以遵循稍微不同的实现来实现相同的结果。

  1. Create two python dictionaries with all the associations letters - first names and letter - last names. 创建两个包含所有关联字母的python词典 - 名字和字母 - 姓氏。
  2. Write them in a file using json . 使用json将它们写在文件中。 This file will substitute yours firstName.txt and lastName.txt 此文件将替换您的firstName.txt和lastName.txt

This should be done only once to create the file with the names. 这应该只进行一次以创建具有名称的文件。

Then your name generator is a script which: 然后你的名字生成器是一个脚本:

  1. Loads those two dictionaries. 加载这两个词典。
  2. Ask the user for an input to obtain the keys. 询问用户输入以获取密钥。
  3. Retrieve the names from the dictionaries using the user input. 使用用户输入从字典中检索名称。

The first two points are implemented in this way: 前两点以这种方式实现:

import json

#these are just brief examples, provide complete dictionaries.
firstnames = {"A": "Crafty", "B": "Brainy"}
lastnames = {"A": "Decapitator", "B": "McBrains"}

with open("fullnames.txt", "w") as ff:
    json.dump(firstnames, ff)
    ff.write('\n')
    json.dump(lastnames, ff)

This would be a script to generate the file with the names. 这将是一个生成带有名称的文件的脚本。

The name generator would be: 名称生成器将是:

import json

with open("fullnames.txt", "r") as ff:
    ll = ff.readlines()
    firstnames = json.loads(ll[0].strip())
    lastnames = json.loads(ll[1].strip())

inputfirst = input("What is your first Name? ")
inputlast = input("What is your last Name? ")

fn = inputfirst[0].upper()
ln = inputlast[-1].upper() #negative indexes start from the last element of the iterable, so -1 would be the last.

print("Your zombie Name is: {} {} ".format(firstnames[fn], lastnames[ln])) #using string format method, better that the old %

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

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