所以我正在用python制作一个石头剪子布游戏,但是有一些新的东西当函数checkifwin()检查是否平局时它也会显示输赢结果
import random
user_input = input('Rock, paper, gun, human, water, air, fire or scissors: ')
rpc_options = ['rock','paper','scissors','fire','gun','human','water','sponge','air']
def checkifwin():
if defeats[user_input] == terminal_response:
print('You win')
if not defeats[user_input] == terminal_response:
print('You lose')
if user_input == terminal_response:
print('We tied')
if user_input not in rpc_options:
print(f'nYour answer is incorrect; it should be in this list: {rpc_options}nYour answer is: {user_input}, do you see the error? Lets try again')
else:
defeats = {
'air' : ['fire', 'rock', 'water', 'gun'],
'gun' : ['rock', 'fire', 'scissors', 'human'],
'paper' : ['air', 'rock', 'water', 'gun'],
'rock' : ['scissors', 'sponge', 'fire', 'human'],
'scissors' : ['air', 'paper', 'human', 'sponge'],
'fire' : ['sponge', 'paper', 'human', 'scissors'],
'water' : ['rock', 'fire', 'scissors', 'gun'],
'sponge' : ['water', 'paper', 'gun', 'air'],
'human' : ['sponge', 'paper', 'air', 'water'],
}
terminal_response = random.choice(rpc_options)
print(f'nYou choose {user_input}, I choose {terminal_response}')
checkifwin()
结果:
Rock, paper, gun, human, water, air, fire or scissors: paper
You choose paper, I choose paper
You lose
We tied
defeats[user_input]
是一个列表。terminal_response
是一个字符串。他们永远不会成为==
。您要使用in
,而不是==
。
您还应该首先测试是否有平局,因为当有平局时,响应将不在defeats
列表中。
if user_input == terminal_response:
print('We tied')
elif terminal_response in defeats[user_input]:
print('You win')
else:
print('You lose')