简体   繁体   English

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

[英]Can't get my python hangman game to count guesses

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.")

The curr_guess currently just prints that they have had 1,2 3 guesses all at the same time i dunno how to do it one at a time, so that when they get the letter wrong it says what guess they are on. curr_guess 目前只打印出他们同时进行了 1,2 3 次猜测,我不知道如何一次做一次,所以当他们把信弄错时,它会说明他们在猜测什么。

Best practices aside you need to set curr_guess variable as a global variable (like word_as_list , word_as_blank , guessed , etc).最佳实践一边需要设置curr_guess变量作为一个全局变量(如word_as_listword_as_blankguessed等)。 Then modify your check_guess like so:然后像这样修改你的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")

I tried that and it works我试过了,它有效

#You have to make cure_guess global 
global cur_guess = 6
#Print out message
print("Sorry this letter is not in the word")
if curr_guess > 0:
    curr_guess -= 1
    print("You still have", curr_guess , guesses) 
else:
    print("You have runned of guesses")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM