简体   繁体   中英

Printing from a dictionary

For a school homework assignment, we have to write a code that will do the following:

Write a program to test your knowledge of the scientific names of animals. Your program should read in these scientific animal names from animals.txt and then ask the user for multiple lines of input. An example animals.txt is shown below:

 Arachnocampa luminosa,Glow Worm Pongo abelii,Sumatran Orang-utan Felis Lynx,Lynx Spheniscus Humboldti,Humboldt Penguin 

animals.txt will contain zero or more lines, each line describing an animal. Each line contains two values, separated by a comma (,). The left hand side of the comma contains the scientific name of an animal, and the right hand side of the comma contains the common name for the animal.

Your program should read in these scientific animal names from animals.txt and then ask the user for multiple lines of input. Each time, your program should ask the user to enter a scientific name of an animal. If this scientific name exists in data you read in from animals.txt, your program should print out the common name for that animal. Otherwise, if the scientific name is unknown, your program should print out that it does not know that animal. For example:

 Scientific name: Spheniscus Humboldti That's the Humboldt Penguin! Scientific name: Callithrix Pygmaea I don't know that animal. Scientific name: Arachnocampa luminosa That's the Glow Worm! Scientific name: 

Below is the code I've written so far. As you guys can see in the example, if the animal is in the list, then it should print out the common name of the animal (not the scientific one). When I run my code, it prints it out correctly for the first two in teh example, but when I input 'Arachnocampa luminosa', it gives out 'That's the Humbolt Penguin'.

animals = {}

for i in open('animals.txt'):
  sName, cName = i.split(',')
  animals[sName] = cName

x = input('Scientific name: ')

while x:
  if x in animals:
    print("That's the " + cName.rstrip() + "!")
  else:
    print("I don't know that animal.")
  x = input('Scientific name: ')

What am i doing wrong that causes this and how do I fix it?

You are doing it wrongly, you are always printing the last value read from the .txt - cName . You should get the information about the common name for the scientific name from the dictionary. Example -

while x:
  if x in animals:
    print("That's the " + animals[x].rstrip() + "!")

Also some suggestions, It is not good to loop over a file like -

for i in open('animals.txt'):

You should explicitly open and close the file, you can use with statement here. Example -

with open('animals.txt') as f:
    for i in f:
        sName, cName = i.split(',')
        animals[sName] = cName

with statement would handle the closing of the file for you, when the block ends.

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