gpt4 book ai didi

Python 刽子手算法

转载 作者:行者123 更新时间:2023-11-28 21:36:20 26 4
gpt4 key购买 nike

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

  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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com