因此,我正在编写一个程序,您在其中捕获一个宠物小精灵并设置它们的等级,当您查询它们时,它会返回它们的等级,但是当我查询它时,它不会返回所选的宠物小精灵,而只是返回字典中的最后一个宠物小精灵
pk = {}
line = input('Command: ')
while line:
tempq = 2
if "Query" in line:
tempq = 1
qparts = line.split()
tempname = parts[1]
if tempname in pk:
print(tempname, "is level", pk.get(tempname),".")
elif tempname not in pk:
print("You have not captured" + tempname + "yet.")
else:
parts = line.split()
name = parts[1]
lvl = parts[tempq]
pk[name] = int(lvl)
line = input('Command: ')
print(pk)
答案 0 :(得分:1)
qparts = line.split()
tempname = parts[1]
您创建了qparts
,但是从不使用它。相反,您引用的是parts
,它是在您的else
块中创建的,其中包含有关在上一个非查询命令中命名的任何神奇宝贝的信息。
请尝试改用tempname
来制作qparts
。
pk = {}
line = input('Command: ')
while line:
tempq = 2
if "Query" in line:
tempq = 1
qparts = line.split()
tempname = qparts[1]
if tempname in pk:
print(tempname, "is level", pk.get(tempname),".")
elif tempname not in pk:
print("You have not captured" + tempname + "yet.")
else:
parts = line.split()
name = parts[1]
lvl = parts[tempq]
pk[name] = int(lvl)
line = input('Command: ')
print(pk)
结果:
Command: catch pikachu 50
Command: catch bulbasaur 10
Command: Query pikachu
pikachu is level 50 .