我不明白这到底是什么意思:a.lower() == x and b.lower() == rules[x]:
rules = {'rock':'scissors', 'scissors':'paper', 'paper':'rock'}
def checkResult(a, b):
if a.lower() == b.lower():
return 'Draw'
for x in rules:
if a.lower() == x and b.lower() == rules[x]:
return 'Player one wins'
else:
return 'Player two wins'
def plyerInputCheck(player):
text = "Player {}, type your choice (Rock, Scissors, Paper): ".format(player)
playerChoice = input(text)
while playerChoice.lower() not in rules:
print("Wrong input, try again!")
playerChoice = input(text)
return playerChoice
while True:
a = plyerInputCheck("One")
b = plyerInputCheck("Two")
print(checkResult(a, b))
answer = input("Play again?")
if answer.lower() in ("n", "no"):
break
我得到了a.lower()==x部分,但是b.lower()==rules[x]到底是什么,特别是rules[x]
在说什么?你知道吗
让我们详细看看。你知道吗
在
rules
中的x
是什么?你有x = "rock"
,x = "scissors"
和x = "papers"
。你知道吗rules
是一个dict
,所以其中有键和值。例如,它将值"scissors"
附加到键"rock"
,因此rules["rock"] == "scissors"
。你知道吗因此,我们在这里检查播放机
a
是否获得一个值为播放机b
拥有的键(在我们的示例中,它将是a
获得"rock"
和b
获得"scissors"
)。你知道吗如果是这样,玩家
a
获胜。你知道吗不过,你真的需要学习更多关于字典的知识,因为这不仅仅是必要的。查看this以了解
dict
对象的基础知识。你知道吗相关问题 更多 >
编程相关推荐