无法让我的python刽子手游戏计算猜测

时间:2015-11-30 10:39:59

标签: python

import random

# DEFINITIONS
name = str(input("Welcome to python hangman! What's your name?"))
print("Hello,", name + "!")
print("You have 6 chances to guess the letter in the word, best of luck!")


def get_guess():
    l = input("Guess a letter!")
    if len(l) != 1 or l not in             'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ':
    print("That is not a letter, try again.")
    get_guess()
elif l in guessed:
    print("You have already guessed that letter, try again.")
    get_guess()
else:
    guessed.append(l)
    return l

def check_guess(l):
for i in range(len(word_as_list)):
    if l == word_as_list[i]:
        word_as_blank[i] = l
if l in word:
    print("This is correct")
else:
    print("sorry this letter is not in the word")
    curr_guess = 0
    while curr_guess != 6:
        if curr_guess < 6:
            curr_guess = curr_guess +1
            print("you have had", curr_guess, "guesses")
        else:
            print("you have run out of guesses")

# VARIABLES AND LISTS


secret_lists = ["chicken", "python", "monkey", "giraffe", "panda", "tiger",  "fire", "christmas", "newspaper", "rudolph", name,"aoife"]  # secret words
word = random.choice(secret_lists)
word_as_list = list(word)
word_as_blank = ["_"]*len(word_as_list)
guessed = []

# MAIN PROGRAM

print(word_as_blank)
while word_as_list != word_as_blank:
   guess = get_guess()
   check_guess(guess)
   print(word_as_blank)
print("GAME OVER – you got the word.")

curr_guess目前只打印他们已经有1,2 3个猜测同时我不知道如何一次做一个,所以当他们得到错误的信时它会说出他们在猜什么。 / p>

2 个答案:

答案 0 :(得分:0)

除了最佳做法,您需要将curr_guess变量设置为全局变量(例如word_as_listword_as_blankguessed等)。然后像这样修改check_guess

...
print("sorry this letter is not in the word")
if curr_guess < 6:
    curr_guess = curr_guess +1
    print("you have had", curr_guess, "guesses")
else:
    print("you have run out of guesses")

答案 1 :(得分:0)

我试过了,它有效

GRANT EXECUTE TO KeoecwqLOCLogin_RecurringAssignmentAlph