简体   繁体   中英

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. 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. You might want to create defaultdict s instead.

Note also that Python has an official style guide that most Python developers follow. 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.
  2. Write them in a file using json . This file will substitute yours firstName.txt and 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 %

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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