所以我在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发布于 2022-05-11 17:11:59
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')https://stackoverflow.com/questions/72205006
复制相似问题