猜字游戏

时间:2017-03-16 09:28:39

标签: python-3.x

我需要帮助修复我的代码这里是我的代码,我的问题是游戏中的生活无法正常工作,代码在你正确猜测时取消生命,当游戏仍有多个时游戏结束生活离开(我使用python):

import random

with open("../Coding.txt") as word_file:
    words = word_file.read().split()

lives = 9

random_word = random.choice(words)
word_len = len(random_word)
guess = ""
attempt = 0
enter = ""
temp = "_" * word_len

lives = 9

print (temp)
for i in range(0,9):
    attempt +=1
    guess = input("Attempt no. "+str(attempt)+":")
    if guess in random_word and guess != enter:
        for i in range(0, word_len):
            if guess == random_word[i]:
                temp = temp[:i] + guess +temp[i+1:]
                print(temp)
                lives + 1
    if guess not in random_word:
            lives -= 1
            print("You have " +str(lives),"lives left")




if "_" not in temp:
        print("Congratulations, You guessed the word")
else:
    print("You ran out of lives please try again!")


enter code here

0 个答案:

没有答案