简体   繁体   English

如果用户输入字母输出应该是单词

[英]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您应该使用字典将人工输入的字母映射到StealDeal

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.

相关问题 输出用户输入的每个单词的第一个字母 - Output first letter of each word of user input 尝试为用户输入的每个字母分配一个表达式,然后打印单词和该单词的总和 - Trying to assign each letter the user inputs with an expression, then print the word and the added total of that word 如何获取用户输入并打印他/她输入的每个单词的字母数? - How can I grab the user inputs and print the letter count of each word he/she typed? 如果用户输入特定单词,则会循环执行的Python程序 - Python program that loops if user inputs particular word 在 python 的文字游戏中打印单词的彩色字母时,不正确的 output - improper output while printing coloured letter of a word in a wordle game in python 正则表达式:output 具有偶数个特定字母的整个单词 - regex: output the whole word having even number of a specific letter 没有 output 当我尝试检查一个单词的每个字母是否在文件中时 - No output when I try to check if each letter of a word is in a file 如何检查用户输入的字母是否在一个单词中 - How do I check if a letter from user input is in a word 当用户输入一个字母时,它会显示隐藏的单词和一些循环 - When a user input a letter it displays the hidden word and some of the loops 如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较? - How to compare user inputted string to a character(single letter) of a word in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM