为什么我的代码不起作用?(简单的“猜单词”——Python游戏)

为什么我的代码不起作用?(简单的“猜单词”——Python游戏),python,Python,我现在学习Python是为了好玩,直到现在一切都很顺利。我正在尝试扩展“猜单词”游戏,例如,让玩家自己选择一个单词(当两个人玩的时候,一个人选择单词,其他人猜),我打赌我的错误在你指出的时候对我来说是显而易见的,但我还是要问。这是密码。我把整个计划都放进去了,即使很难,也只有最重要的部分才重要。我只是把剩下的放进去,因为它不多,也许你们可以更好地理解它 print("Do you wish to set the Word yourself, or let the program choose?"

我现在学习Python是为了好玩,直到现在一切都很顺利。我正在尝试扩展“猜单词”游戏,例如,让玩家自己选择一个单词(当两个人玩的时候,一个人选择单词,其他人猜),我打赌我的错误在你指出的时候对我来说是显而易见的,但我还是要问。这是密码。我把整个计划都放进去了,即使很难,也只有最重要的部分才重要。我只是把剩下的放进去,因为它不多,也许你们可以更好地理解它

print("Do you wish to set the Word yourself, or let the program choose?")
user_input = input("1 for own Input - 0 for Program-Input")
if user_input == 1:
    Keyword = input("Type in the Word you want to use!")
else:
    Keyword = "castle"
    word = list(Keyword)
    length = len(word)
    right = list ("_" * length)
    used_letters = list()
    finished = False
    while finished == False:
        guess = input("Guess a Letter!")
        if guess not in Keyword:
            print("This letter is not in the word. Sorry...")
        for letter in word:
            if letter == guess:
                index = word.index(guess)
                right[index] = guess
                word[index] = "_"
        if guess in used_letters[0:100]:
            print("You already used that letter before!")
        else:
            used_letters.append(guess)
            list.sort(used_letters)
        print(right)
        print("Used letters:")
        print(used_letters)
        if list(Keyword) == right:
            print("You win!")
            finished = True
input('Press ENTER to exit')

我的问题是,我想添加一个函数,以便您可以选择是自己设置一个单词,还是使用程序定义为“关键字”的单词。但无论我输入什么,它总是以“猜字母”开头,而不是跳到程序设置关键字的地方。提前感谢您的回答!:)

您的代码有两个问题

  • 将整个代码块放入
    else
    语句中。这意味着如果执行了
    if user\u input==1:
    块,您将只向用户请求一个单词,然后程序将结束,因为
    else
    语句将被跳过

  • 如果用户输入=1,则使用
    作为检查,这永远不会是真的,因为用户输入总是作为字符串读入。字符串1永远不会等于整数1。这就是为什么程序总是跳转到else语句。如果int(用户输入)==1,则需要执行


  • 无论何时使用
    input
    函数收集用户输入,它都是
    字符串,而不是
    int
    。这意味着您必须将该值解析为
    int
    或使用字符串对其求值

    选项1:解析到
    int

    user_input = int(input("1 for own Input - 0 for Program-Input"))
    
    选项2:使用
    字符串进行计算

    if user_input == "1":    
    

    input
    返回的字符串不是整数,因此它永远不能等于1,而是等于
    “1”
    。 另外,用于用户猜测的代码仅在程序选择单词时运行,因此它需要不插入

    请注意,您的代码当前注册的大写字母与小写字母不同,您可以通过在每次输入后放置一个
    .lower()
    ,将所有大写字母转换为小写字母来解决此问题

    print("Do you wish to set the Word yourself, or let the program choose?: ")
    user_input = input("1 for own Input - 0 for Program-Input")
    if user_input == "1":
        Keyword = input("Type in the Word you want to use: ").lower()
    else:
        Keyword = "castle"
    word = list(Keyword)
    length = len(word)
    right = list ("_" * length)
    used_letters = list()
    finished = False
    while finished == False:
        guess = input("Guess a Letter: ").lower()
        if guess not in Keyword:
            print("This letter is not in the word. Sorry...")
        for letter in word:
            if letter == guess:
                index = word.index(guess)
                right[index] = guess
                word[index] = "_"
        if guess in used_letters[0:100]:
            print("You already used that letter before!")
        else:
            used_letters.append(guess)
            list.sort(used_letters)
        print(right)
        print("Used letters:")
        print(used_letters)
        if list(Keyword) == right:
            print("You win!")
            finished = True
    input('Press ENTER to exit')
    

    您应该使用命名约定命名变量,
    关键字
    而不是
    关键字
    输入
    返回字符串,而不是整数。所以你应该测试
    如果用户输入==“1”:
    你可以用它来可视化你的程序并发现错误。欢迎来到堆栈溢出。在发布问题之前,请花时间阅读提问指南。hank您不知道:)您可以将共享代码移到任何缩进块之外……这样您就可以使用if…else来选择关键字,然后代码的其余部分将始终执行。