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

2024-04-19 15:20:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在学习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')

我的问题是,我想添加的功能,能够选择如果你想自己设置一个词,或使用的话,程序已经定义为“关键字”。但是不管我输入什么,它总是以“猜一个字母”开始,而不是跳到程序设置关键字的地方。提前感谢您的回答!:)


Tags: thein程序rightinputindexifkeyword
3条回答

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')

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

选项1:解析到int

user_input = int(input("1 for own Input - 0 for Program-Input"))

选项2:使用string进行评估:

if user_input == "1":    

你的代码有两个问题。你知道吗

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

  2. 您正在使用if user_input == 1:作为检查,这永远不会是真的,因为用户输入总是作为字符串读入。字符串1永远不会等于整数1。这就是为什么程序总是跳转到else语句。你需要做if int(user_input) == 1:

相关问题 更多 >