如何打印与用户输入相对应的字典元素

发布于 2025-01-17 04:08:50 字数 2070 浏览 2 评论 0原文

在这个问题中,我的 file.txt 看起来像

Tanzania:2948
Lesotho:2932
Comoros:2751
Nepal:2702
Yemen:2606
Zimbabwe:2434
Solomon Islands:2427
Benin:2276
Guinea:2247
Mali:2218
Kiribati:2185
Rwanda:2043
Afghanistan:1976
Chad:1945
Ethiopia:1903
Uganda:1868
Burkina Faso:1866

等。

我当前的代码看起来像

def makeDicts():
 incomeDict = dict()
 countryDict = dict()

 countryList =[]
 gdpList = []
 initialList = []

 with open('lab8.txt','r') as f:
  for line in f:
   line= line.upper().strip().split(':')
   countryList.append(line[0])
   gdpList.append(line[1])
   initial =line[0][0]
   initialList.append(initial)

  for i in range(len(countryList)):
   incomeDict[countryList[i]] = gdpList[i]

   if initialList[i] not in countryDict:
    countryDict[initialList[i]] = set()
   countryDict[initialList[i]].add( countryList[i])

  return (incomeDict,countryDict)

userinp = input('Enter File Name: ')
incomeDict , countryDict = makeDicts()


while True:
 x = input("Enter a Coutry name or letter or DONE to quit: ")
 x = x.upper()

 if x=='DONE':
  exit()

我知道该函数是正确制作的,因为它是根据确切的指令制作的,但我需要制作我遇到问题的较低程序是任务。

在本练习中,我们将编写一个使用两个字典的程序。该计划 将能够执行两种不同的功能:

  1. 如果输入国家/地区名称,程序应报告人均国内生产总值 该国的产品(GDP)。
  2. 如果输入字母,程序应报告该字母中包含的所有国家/地区 以该字母开头的系统。

最后,规范要求我们提示用户输入一个首字母缩写,返回以该首字母开头的国家/地区集合,或者输入一个返回相应收入的国家/地区名称,或者输入“完成”一词,退出程序。我们可以使用 while 循环来完成此操作,该循环将运行直到用户输入“DONE”。它将检查用户输入的文本是否在任何字典中,并返回该键的适当值。将其写入 main() 函数,并给出以下建议:

  1. 首先,要构造字典,编写一行代码,如 IncomeDict , CountryDict = makeDicts()

  2. 编写 while 循环并重复提示用户输入。

  3. 将输入转换为大写后,您应该测试用户是否输入了国家/地区名称或首字母缩写。这些都可以通过测试用户输入是否是其中一本词典中的键来完成。例如,如果用户输入位于名为 x 的变量中,则“x in countryDict”将测试用户输入的字母是否是一个或多个国家/地区名称的开头。

  4. 如果用户输入字母,则报告以该字母开头的所有国家/地区:这些国家/地区以 F 开头:{'FINLAND', 'FIJI', 'FRANCE'}

  5. 如果用户输入国家/地区名称,报告该国家的人均GDP:芬兰的人均GDP为46344

  6. 如果用户输入“完成”,则退出程序。

  7. 对于所有其他输入,报告错误并再次提示。

你能帮我完成这个吗?

我已经尝试了上面写的代码,但我不知道如何扫描字典并根据说明生成输出。

我不知道我可以使用什么类型的函数或运算符?

in this problem, i have file.txt that looks like

Tanzania:2948
Lesotho:2932
Comoros:2751
Nepal:2702
Yemen:2606
Zimbabwe:2434
Solomon Islands:2427
Benin:2276
Guinea:2247
Mali:2218
Kiribati:2185
Rwanda:2043
Afghanistan:1976
Chad:1945
Ethiopia:1903
Uganda:1868
Burkina Faso:1866

etc.

my current code looks like

def makeDicts():
 incomeDict = dict()
 countryDict = dict()

 countryList =[]
 gdpList = []
 initialList = []

 with open('lab8.txt','r') as f:
  for line in f:
   line= line.upper().strip().split(':')
   countryList.append(line[0])
   gdpList.append(line[1])
   initial =line[0][0]
   initialList.append(initial)

  for i in range(len(countryList)):
   incomeDict[countryList[i]] = gdpList[i]

   if initialList[i] not in countryDict:
    countryDict[initialList[i]] = set()
   countryDict[initialList[i]].add( countryList[i])

  return (incomeDict,countryDict)

userinp = input('Enter File Name: ')
incomeDict , countryDict = makeDicts()


while True:
 x = input("Enter a Coutry name or letter or DONE to quit: ")
 x = x.upper()

 if x=='DONE':
  exit()

I know the function is made correctly as it was made based on exact instructions but I need to make the lower program which I am having trouble with these are the tasks.

For this exercise, we will write a program that makes use of two dictionaries. The program
will be able to do two different functions:

  1. If a country name is entered, the program should report the per capita gross domestic
    product (GDP) of that country.
  2. If a letter is entered, the program should report all the countries that it has in the
    system that start with that letter.

Finally, the specifications ask us to prompt the user for either an initial which returns the set of countries that start with that initial, or a country name that returns the corresponding income, or the word “DONE” which exits the program. We can accomplish this using a while loop that will run until the user enters “DONE”. It will check if the user entered text is in any of the dictionaries and return the appropriate value at that key. Write this into a main() function, with the following suggestions:

  1. First, to construct the dictionaries, write a line of code like incomeDict , countryDict = makeDicts()

  2. Write a while loop and repeated prompt the user for input.

  3. After converting the input to upper-case, you should test whether the user typed in a country name or an initial. These can both be accomplished by testing whether the user input is a key in one of the dictionaries. For instance, if the user input is in a variable called x, then ‘x in countryDict’ will test if the user typed in a letter that is the start of one or more country names.

  4. If the user types in a letter, report all of the countries that start with that letter: These countries start with F: {'FINLAND', 'FIJI', 'FRANCE'}

  5. If the user types in a country name, report the per capita GDP of that country: FINLAND has per capita GDP of 46344

  6. If the user types in DONE, then quit the program.

  7. For all other inputs, report an error and prompt again.

can you please help me finish this

I have tried the code that I wrote above but I do not know how to scan the dictionarys and produce the output based on the instructions.

I do not know what type of functions or opperators I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2025-01-24 04:08:50

if x=='DONE': 行之后,您会想说:

 if x == 'DONE':
  exit()
 elif len(x) == 1:
  if x in countryDict:
   print(f"These countries start with {x}: {countryDict[x]}")
  else:
   print(f"No contries start with {x}")
 else:
  if x in incomeDict:
   print(f"{x} has per capita GDP of {incomeDict[x]}")
  else:
   print(f"Country {x} not registered")

作为旁注,太小的缩进可能会使代码可读性较差(恕我直言)。

After the if x=='DONE': line, you will want to say as:

 if x == 'DONE':
  exit()
 elif len(x) == 1:
  if x in countryDict:
   print(f"These countries start with {x}: {countryDict[x]}")
  else:
   print(f"No contries start with {x}")
 else:
  if x in incomeDict:
   print(f"{x} has per capita GDP of {incomeDict[x]}")
  else:
   print(f"Country {x} not registered")

As a side note, the too small indentation may make the code less readable (IMHO).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文