我想从dict中获取所选选项并进行操作,但是当我打印变量时,它不会打印任何内容。
def play():
switcher = {
1: "Rock",
2: "Paper",
3: "Scissors"}
print(switcher)
i = input("Enter Your Choice: ")
s = switcher.get(i)
print(s)
play()
答案 0 :(得分:0)
调用switcher.get(i)
时,变量i
是一个字符串,而应该是一个整数。这说明了您的问题。
作为解决方案,我建议您首先检查输入值是否正确(如果不正确,请重复该问题),然后将其转换为int,如下所示:
def play():
switcher = {
1: "Rock",
2: "Paper",
3: "Scissors"}
print(switcher)
while True:
i = input("Enter Your Choice: ")
if i in ["1", "2", "3"]:
i = int(i)
break;
s = switcher.get(i)
print(s)
play()