首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >石头剪刀,如果领带不起作用

石头剪刀,如果领带不起作用
EN

Stack Overflow用户
提问于 2022-05-11 17:07:04
回答 1查看 35关注 0票数 0

所以我在python中做了一个石头剪刀游戏,但是有了一些新的东西,当函数checkifwin()检查领带时,它也会将胜负结果放上去。

代码语言:javascript
复制
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()

如果结果一致:

代码语言:javascript
复制
Rock, paper, gun, human, water, air, fire or scissors: paper

You choose paper, I choose paper
You lose
We tied
EN

回答 1

Stack Overflow用户

发布于 2022-05-11 17:11:59

defeats[user_input]是一个列表。terminal_response是一个字符串。他们永远不会是==。您想要使用in,而不是==

您还应该首先测试一个领带,因为当有一个绑定时,响应将不在defeats列表中。

代码语言:javascript
复制
if user_input == terminal_response:
    print('We tied')
elif terminal_response in defeats[user_input]:
    print('You win')
else:
    print('You lose')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72205006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档