[英]If user inputs letter output should be word
Basically, my game runs off 3 words - Steal, Deal or Quit however I want the option that if the user inputs say the letter 's' it should = Steal as the output (The player is versing a PC and the results of the game are based on the 2 values.基本上,我的游戏运行了 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')
You should use a dictionary to map the human letter input to the words Steal
or Deal
您应该使用字典将人工输入的字母映射到Steal
或Deal
词
First add the dictionary at the top:首先在顶部添加字典:
human_input_map = {
's': 'Steal'
'd': 'Deal'
}
Then after taking the user input you can convert their input into the full word for comparing with computer_choice
.然后在接受用户输入后,您可以将他们的输入转换为完整的单词以与computer_choice
进行比较。
human = input('Steal, Deal or Quit [s|d|q]?:')
human = human_input_map[human]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.