Python 刽子手算法

标签 python python-3.x

所以,当我为了好玩而编写一个简单的刽子手游戏时,由于我是初学者,我仍然需要一些帮助:

  1. 我希望我的算法能够测试输入是否只有一个字符,例如:

    while True:
      x=str(input())
      #code checking wheather it is only one character, if it is  
      character break and stop the loop, 
      otherwise repeat the input
    
  2. 假设我的 secret 词是'billy' 我想知道如何检查这个输入的字母是否与 secret 单词中的任何一个字母相同。例如

    if x *code checking if the input has a same letter as the secret word*:
      #carry on program
    

如果您能帮助我解决这两个问题中的任何一个,您将成为我的救星!

最佳答案

检查时可以使用if in。因此,您获得输入,然后使用如下代码:

if x in secret_word:
  #carry on with the program

要检查它是否有一个字符,请使用 len。所以

if len(x)==1:
  #carry on

关于Python 刽子手算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197005/

相关文章:

python - 从字典列表中获取值列表?

c++ - 导入使用 gcc 4.4.3 编译的 C++ 中的 python 模块

python - Pandas Pyqt 实现表过滤排序

Python从无限循环线程返回值

android - Kivy - 在任何安卓设备上获取 DCIM 文件夹的路径

python - 使用类在 python 中创建图形数据结构

python - 从Python执行shell命令并合并环境更改(没有子进程)?

Python 转换为 ISO-8859-5

python - 如何安全使用exec?我想使用动态加载的代码模块作为配置文件

python - 模块 'importlib' 没有属性 'util'