简单的hang man游戏,该游戏会给我带来问题,使用st击和替换

时间:2018-08-02 02:45:36

标签: python

你好,我现在正在苦苦挣扎,我是代码新手,只想看看我可以用我的代码解决什么。它是一个简单的游戏。我只是一直收到0条回复。我打算添加多个级别,例如easy,med和hard dif。我试图修复该代码,但由于我是新手,所以无法。有人请阐明一下。

import random

answerlist = ["bap" , "wow" , "ok" , "no" ]

random.shuffle(answerlist)

answer = list(answerlist[0])

#print(answer)

#empty list called display
display = []

#adds the variable answer to display
display.extend(answer)

#print(used)

#interates through the list 'display'

for i in range (len(display)):
            #replaces each index in the list with '__'
            display[i] ="_"
# the join command puts a space between each "__"
print (' ' .join(display))
print ()

#counter stops the game once all letters guessed
count  = 0

#keeps asking user untill all letters guessed

while count < len(answer):
                guess = input("please take a guess: ")
                guess = guess.lower()
                print (count)

# iterates through the letters in answer

for i in range(len(answer)):

#if  the guessed letter matches the letter
# in the answer

                if answer [i] == guess :
# replace the index of that guess with
# the actual letter they guessed
                             display[i] = guess
                count = count + 1

                #print (used)

#print out the new string with guessed letters in
                print (' '.join(display))
                print()

print("well done , you have won")

2 个答案:

答案 0 :(得分:0)

代码非常混乱,因此很难说出问题的实质。几点建议: 1.为什么稍后在代码中执行display.extend(answer)并将显示设置为_?我看不到它的作用。 2. count = count + 1仅在用户正确输入字母后才应激活?每次检查字母时,语法数量都会增加

此外,请以正确的语法粘贴您的代码,以便更轻松地进行调试。

答案 1 :(得分:0)

除了格式外,我很确定您的问题来自此部分:

while count < len(answer):
    guess = input("please take a guess: ")
    guess = guess.lower()
    print (count)

count设置为0且永不更改,因此while循环将永远持续下去。