默认情况下,
nc = dict(zip(nation,cap))
print("Countries and Capitals :{}".format(nc))
k = 0
while k != 5:
k = input("input : ")
if k == 1:
break
if k != 1:
key = k
print("The capital of {} is {} ".format(key,nc[key]))
#这只会在我键入1时出错。我希望它在按下1时停止程序。我该怎么办?
input("input ")
返回一个字符串。
你可以做:
nc = dict(zip(nation,cap))
print("Countries and Capitals :{}".format(nc))
k = 0
while k != 5:
k = int(input("input : "))
if k == 1:
break
if k != 1:
key = k
print("The capital of {} is {} ".format(key,nc[key]))
input()
返回字符串
因此,您需要使用int()
函数将k作为整数输入。
k = int(input("input..."))