【问题标题】:If user inputs letter output should be word如果用户输入字母输出应该是单词
【发布时间】:2021-10-16 03:50:00
【问题描述】:

基本上,我的游戏运行 3 个字 - Steal、Deal 或 Quit 但是我想要一个选项,如果用户输入说字母 's' 它应该 = Steal 作为输出(玩家正在使用 PC 和结果游戏的数量基于 2 个值。

human = input('Steal, Deal or Quit [s|d|q]?:')

print(" ")

print('You chose: ' + human)

#Computer input
     
sd = ["Steal", "Deal"]
computer_choice = random.choice(sd)

print('Comp chose: ' + computer_choice)

print(" ")

if human == computer_choice:
    print('Draw! Split pot -50 each')
elif human == 'Steal' and computer_choice == 'Deal':
    print('You win! You gain 100.')
elif human == 'Deal' and computer_choice == 'Steal':
    print('You loose! Comp gain 100.')
elif human == 'Steal' and computer_choice == 'Steal':
    print('Too Greedy! You get nothing')

【问题讨论】:

  • 为什么quit没有代码?
  • 请在未来发送minimal reproducible example。 90% 的代码与问题无关。
  • 那是我的错,我复制粘贴了两次,不是故意重复。我只是一步一步地完成它,退出代码是下一步!感谢您提供的最小可重现示例将在未来提供很好的帮助!

标签: python variables input


【解决方案1】:

您应该使用字典将人类字母输入映射到单词StealDeal

首先在顶部添加字典:

human_input_map = {
    's': 'Steal'
    'd': 'Deal'
}

然后在获取用户输入后,您可以将他们的输入转换为完整的单词,以便与computer_choice 进行比较。

human = input('Steal, Deal or Quit [s|d|q]?:')

human = human_input_map[human]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2020-12-08
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多