【问题标题】:Hangman game - issues with looping刽子手游戏 - 循环问题
【发布时间】:2016-10-29 16:50:09
【问题描述】:

我正在尝试设计一个简单的刽子手游戏,但目前在循环这个游戏时遇到了问题。我对 Python 非常陌生,并且知道这可能存在一个非常简单的问题,但希望能提供任何帮助。

这是我目前拥有的游戏代码:

import random
random_words = ["stationery", "notepad", "pencil", "paper","eraser","highlighter","stapler","sharpener"]
computer_choice = random.choice(random_words)
print("The number of letters in the word I have chosen is " +           str(len(computer_choice) + ".")

player_guess = None
guessed_letters = []
word_guessed = []

for letter in computer_choice:
    word_guessed.append("-")
    joined_word = None

player_guess = str(input("Please pick a letter you think is in the word I have chosen."))

attempts = (len(computer_choice)-1)

for letter in (0, len(computer_choice)):

if attempts != 0 and "-" in word_guessed:
    joined_word = "".join(word_guessed)
    print(joined_word)

    guessed_letters.append(player_guess)

    for letter in range(len(computer_choice)):
            if player_guess == computer_choice[letter]:
                word_guessed[letter] = user_input

            if player_guess not in computer_choice:
                attempts -= 1
                player_guess = str("Please try again. You have  " + str(attempts) + " attempts remaining.")

if "-" not in word_guessed:
print("Congratulations! {} was the word").format(computer_choice)

else:
print("Unlucky! The word was " + str(computer_choice) + "!")

目前,游戏没有循环播放,只是直接切换到“倒霉,这个词是___”。我该如何解决?问题是什么?

【问题讨论】:

  • 你能仔细检查你的缩进吗?例如,if attempts != 0 and "-" in word_guessed: 出现在 for letter in (0, len(computer_choice)): 的正下方,最后的 if/else bock 似乎也很无聊(至少乍一看)

标签: python python-3.x loops


【解决方案1】:

当您发布代码时,请对其进行标识,以便更容易理解并让 python 编译,因为这在解释器中是必不可少的。

您的代码在循环中出现了一些错误,例如不要使用 for 循环来循环简单的重复,使用 for 来循环列表。对于简单的重复使用while。在两个嵌套的 for 循环中也是相同的变量,让事情变得复杂。

也无需在另一个 for 循环中循环遍历每个字母,in 运算符将已经检查单词中是否存在字符。

我创造了另一种方法来查找和替换单词中的字符,我想更简单一点

import random
random_words = ["stationery", "notepad", "pencil", "paper","eraser","highlighter","stapler","sharpener"]
computer_choice = random.choice(random_words)
print("The number of letters in the word I have chosen is " + str(len(computer_choice)))
win = computer_choice #computer_choice will be destroyed later on
print(computer_choice)
guessed_letters = []
word_guessed = []

for letter in computer_choice:
    word_guessed.append("-")
joined_word = None

player_guess = input("Please pick a letter you think is in the word I have chosen.")

attempts = (len(computer_choice)+1)

x = 0
while x < len(computer_choice):
    x+=1

    if attempts != 0 and "-" in word_guessed:

        if player_guess in computer_choice:
            y=0
            while y < computer_choice.count(player_guess): #will count how many times guessed word is in string
                y+=1
                pos = computer_choice.find(player_guess) #return index of where the letter is
                word_guessed[pos] = player_guess #replaces word_guessed with letter in pos
                computer_choice = computer_choice.replace(player_guess,'#',1) #deletes so it won't find it again
            player_guess = "/"
        if player_guess not in computer_choice:
            attempts -= 1
            print("Please try again. You have  " + str(attempts) + " attempts remaining.")
        player_guess = input("Please pick a letter you think is in the word I have chosen.")
        joined_word = "".join(word_guessed)
        print(joined_word)
    else:
        break

if "-" not in word_guessed:
    print("Congratulations! %s was the word"%win)

else:
    print("Unlucky! The word was " + str(win) + "!")

【讨论】:

    猜你喜欢
    • 2016-02-13
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多