我正在为称为编程入门的课程做一些功课。 我正在制作一个带有字典列表的小型查找应用程序。 我只是想知道我能做些什么,以便用户可以输入“ pubg”而不是“ PUBG”?我在所有地方都尝试过.lower(),但是因为我不太了解它的工作原理,所以我不知道需要更改什么。
import sys
while True:
print("-" * 45)
print("\n" "Welcome to game creator lookup!")
game_list = {
"Battlefield" : "EA DICE",
"Fortnite" : "Epic Games",
"Counter strike" : "Valve",
"PUBG" : "PUBG Corporation",
"Overwatch" : "Blizzard",
"Dota" : "Valve",
"Call of duty" : "Infinity Ward",
"The sims" : "EA, Maxis, Firemoney studios"}
choice = (input("What game would you like to look up? (Type 'q' to quit) " "\n"))
if choice in game_list:
print (choice, "was created by", game_list[choice])
if choice == ("q"):
print("goodbye")
sys.exit()
elif choice not in game_list:
print("Invalid choice, try again")
答案 0 :(得分:1)
以这种方式重写游戏列表:
game_list = {
"battlefield" : ("Battlefield", "EA DICE"),
"fortnite" : ("Fortnite", "Epic Games"),
"counter strike" : ("Counter strike", "Valve"),
"pubg" : ("PUBG", "PUBG Corporation"),
...
(实际上,此重写可以使用适当的功能自动完成)
然后您就可以使用
choice = choice.lower()
if choice in game_list:
print (game_list[choice][0], "was created by", game_list[choice][1])