所以我正在制作一个摇滚纸牌游戏,我添加了我的源代码。到目前为止,我已经让球员进入他们的选择。我尝试添加一个检查,以确保他们输入的选项是三个选项之一。我的程序修正了一次,然后它停止做任何事情
这是我的源代码
print('''Please pick one of the following:
Rock
Paper
Scissors''')
p1 = None
p2 = None
while True:
gameDict = {"rock":1,"paper":2,"scissors":3}
in1 = input("Player 1: ").lower()
in2 = input("Player 2: ").lower()
p1 = gameDict.get(in1)
p2 =gameDict.get(in2)
while gameDict.get(p1)==None or gameDict.get(p2)==None:
if(p1==None):
p1=input("Player 1, please enter one of the choices listed above: ")
elif p2== None:
p2=input("Player 2, please enter one of the choices listed above: ")
print('Done!!')
print(p1,p2)
并不是说它什么都没做。事实上,它做了很多,它在一个无限循环中。输入错误输入时会发生以下情况:
p1 = None
,p2 = None
李>p1 == None
的计算结果为true
,它执行if
语句,一个新值被分配给p1
,现在它不再是None
李>p2 == None
的计算结果为true
,它执行if
语句,一个新值被分配给p2
,现在它不再是None
李>p1
和p2
都不是None
,因此if
语句都不会执行,循环会无限迭代李>我建议你做以下几点:
我已经修好的东西:
val
李>elif
更改为if
,因为其中一个玩家的输入可能有效,但另一个玩家的输入无效,并且您希望循环,直到两者都有效李>我认为这是正确的代码: (您应该检查p1或p2是否为None)
您可以将用户(“剪刀”)输入的键与字典中的值进行比较。您需要确保钥匙是正确的:
获得有效输入后,您可以查找它们:
相关问题 更多 >
编程相关推荐