我必须创建一个程序,该程序接受用户输入的州,并返回该州的花。我必须阅读的以下文本文件称为“ state_flowers.txt”,其中包含以下数据
California,Poppy
West Virginia,Rhododendron
South Dakota,Pasque Flower
Connecticut,Mountain Laurel
New York,Rose
Georgia,Cherokee Rose
Washington,Coast Rhododendron
Virgina,American Dogwood
Arizona,Saguaro Cactus
Hawaii,Pua Aloalo
Alabama,Camelia
Illinois,Violet
Indiana,Peony
Delaware,Peach Blossom
Iowa,Wild Prairie Rose
Kansas,Sunflower
Alaska,Forget Me Not
Lousiana,Magnolia
Maine,White Pine Tassel
Massachusetts,Trailing Arbutus
Michigan,Apple Blossom
Minnesota,Lady Slipper
Mississippi,Magnolia
Missouri,Hawthorn
Montana,Bitterroot
Nebraska,Goldenrod
Nevada,Sagebrush
New Hampshire,Lilac
New Jersy,Violet
New Mexico,Yucca Flower
etc......
我在代码中遇到的问题是,它只会要求输入州名,然后一遍又一遍地继续进行而没有任何输出。到目前为止,这是我所拥有的代码:
d = {}
myFile = open('state_flowers.txt', 'r')
for line in myFile:
line2=line.split(",")
state = line2[0]
flower = line2[1]
c = len(flower)-1
#Strips the new line symbol
flower = flower[0:c]
d[state] = flower
#matches each state with its flower
for state, flower in d.items():
search = input("Enter state name:") #user enters input of state
if state == search:
print(flower, "is the State Flower for", state)
正如我所说,我的程序所要求的只是一遍又一遍的输入。因此它是这样的:
Enter state name:Maine
Enter state name:Califorina
Enter state name:Texas
Enter state name:
Enter state name:
我觉得自己在这方面非常接近,希望能对您有所帮助,并且对我所做的不正确操作给予清晰的解释,将不胜感激!谢谢!
答案 0 :(得分:7)
您很近。无需重复字典。 dict
的美丽之处在于它提供了O(1) access to values给定的密钥。您只需输入您的内容并将密钥提供给字典:
search = input("Enter state name:") #user enters input of state
print(d.get(search), "is the State Flower for", search)
在Python 3.6及更高版本中,您可以使用f字符串更清晰地编写此代码:
print(f'{d.get(search)} is the State Flower for {search}')
如果字典中不存在该状态,则d.get(search)
将返回None
。如果您在这种情况下不想打印任何内容,则可以使用if
语句:
search = input("Enter state name:") #user enters input of state
if search in d:
print(f'{d[search]} is the State Flower for {search}')
答案 1 :(得分:1)
您的问题是您的代码中
for state, flower in d.items():
search = input("Enter state name:") #user enters input of state
if state == search:
print(flower, "is the State Flower for", state)
您遍历所有状态/花朵对,并每次都要求输入状态名称。因此,如果您有五十个状态/花朵对,将询问用户五十次。这不是你想要的。
相反,将包含input(...)
语句的行移动到循环的 outside 之外。用什么方式,直到请求之后,循环才会开始。
关于输入行和循环:
search = input("Enter state name:") #user enters input of state
for state, flower in d.items():
if state == search:
print(flower, "is the State Flower for", state)
考虑将其替换为三个非循环行:
state = input("Enter state name: ")
flower = d[state]
print(flower, "is the State Flower for", state)
就是这样。无需手动搜索循环,因为dict对象会搜索您。
如果您担心用户输入了错误的州名,并且不想让程序抛出异常,则可以将flower = d[state]
行更改为:
flower = d.get(state, 'Nothing')
d.get(state)
的工作方式与d[state]
几乎相同,不同之处在于,如果{{},则可以指定将flower
设置为什么(在本例中为"Nothing"
)。 1}}在字典中找不到。
答案 2 :(得分:0)
您可以使用Python 3.6+简单地做到这一点:
print(f'{d.get(search)} is the State Flower for {search}')
答案 3 :(得分:-1)
您可以尝试简单的调试。在比较条件之前打印“状态”和“搜索”的值。这种情况没有得到“正确”,因此它只是为用户输入而迭代:
for state, flower in d.items():
search = input("Enter state name:") #user enters input of state
if state == search:
print(state,search)
print(flower, "is the State Flower for", state)