简体   繁体   English

从字典打印

[英]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. 您的程序应从animal.txt中读取这些科学的动物名称,然后要求用户输入多行。 An example animals.txt is shown below: 下面显示了animals.txt示例:

 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. animals.txt将包含零行或更多行,每行描述一个动物。 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. 您的程序应从animal.txt中读取这些科学的动物名称,然后要求用户输入多行。 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. 如果您从animal.txt读取的数据中存在该科学名称,则程序应打印出该动物的通用名称。 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'. 当我运行我的代码时,在示例中,它会正确打印出前两个代码,但是当我输入“ Arachnocampa luminosa”时,它会给出“ That's 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 . 您做错了,您总是打印从.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. 您应该显式打开和关闭文件,您可以在此处使用with语句。 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. with语句将在块结束时为您处理文件的关闭。

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

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