简体   繁体   English

我如何检查字母是否是随机单词?

[英]How would I check if a letter was in a random word?

I am creating a game, and it entails picking a random word from a list, then asking the user for a letter input. 我正在创建一个游戏,它需要从列表中选择一个随机单词,然后要求用户输入字母。 I have this. 我有这个。 I now need to check if the letter input is in the random word. 现在,我需要检查字母输入是否在随机单词中。 How would I do this? 我该怎么做? I don't have Python on my computer so I can't check if my code works until tomorrow(this is a school project so to check my code I use the school computers). 我的计算机上没有Python,所以直到明天我才能检查我的代码是否有效(这是一个学校项目,因此要使用学校计算机检查我的代码)。 I'm not exactly sure where to start with this, so any guidance would be much appreciated, as I am fairly new to Python(as you may have guessed). 我不确定从哪里开始,所以任何指导都将受到赞赏,因为我对Python还是相当陌生的(您可能已经猜到了)。

You've got a word (variable word ), you've got a letter (variable letter ), so go with a simple test to ask if a letter is in the word: 您有一个单词(变量word ),有一个字母(变量letter ),因此,请通过一个简单的测试来询问单词中是否包含字母:

if letter in word:
    print('Yes, {} is in the word'.format(letter))  # or whatever message/outcome you want 
else:
    print ('Nope, not there.')

That's about it - you can add this in to your existing code, change variable names to match what you have, etc. 就是这样-您可以将其添加到现有代码中,更改变量名称以匹配您的名称,等等。

对于简单的编码问题,您可以使用在线repl: https ://repl.it/languages/python3您计算机上的Python,而无需安装它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM