繁体   English   中英

我只能输入 1 个字母,如何让代码知道 2 个或更多字母也可以?

[英]I only can type 1 letter , how do I let the code know that 2 or more letters are also okay?

import random
from typing import List
words =  [ 'apple', 'tree', 'python', 'bench', 'float' ]
word = random.choice(words) 
guesses= []
letter_storage = []
f = "Do you want to play?"
a = ["yes","no"]
max_fails = []

def print_word_to_guess(letters: List):

    print("{0}".format(" ".join(letters))) # need to display _ _ _ _ instead of ['_', '_', '_', '_', '_'] 

def input_choice(f:str,a:list[str])->str:

    if f in a:
        if f == "yes":
            a = "yes"
        elif f  == "no":
            print("ok no")
    elif f not in a:
        while f not in a:
            f = input("Invalid answer. Try again")
            if f in a:
                if f == "yes":
                    a = "yes"
                elif f  == "no":
                    print("ok no")

def shape(word:str,guesses:str)->str:
    for letter in word:
        guesses.append("_ ")

def hangman(word:str,max_fails:int):

    max_fails = int(input("Number of allowed mistakes: "))
    while max_fails> 0:
        guess = input("make a guess:")
        if guess in letter_storage: 
            print("mistakes left:",max_fails,",you already guessed that letter!")    
        else: 
            letter_storage.append(guess)
            if guess in word :
                for x in range(0, len(word)): 
                    if word[x] == guess:
                        guesses[x] = guess
                print_word_to_guess(guesses)

                if not '_ ' in guesses: 
                    print("You won!","The word was:",word)
                    break
            else:
                max_fails -= 1
                print(max_fails,"mistakes left")

                if max_fails == 0:
                    print("You lost :( The word was",word)

input_choice(input("Do you want to play? [yes / no]"),("yes","no"))  
shape(word,guesses)
hangman(word,max_fails)

这是我的小刽子手游戏。 该代码识别出单词中有多个字母,但不会将它们添加到空格中。 我希望多个字母也被接受,所以当你已经猜到应用程序时,你也可以添加 le 来完成游戏。 如何在我的代码中实现这一点?

您可以将猜测保存到变量guess_str中,然后for guess in guess_str猜测的所有字母并再次运行您的代码逻辑:

def hangman(word: str, max_fails: int):
    max_fails = int(input("Number of allowed mistakes: "))
    while max_fails > 0:
        guess_str = input("make a guess:")
        for guess in guess_str:
            if guess in letter_storage:
                print("mistakes left:", max_fails, ",you already guessed that letter!")
            else:
                letter_storage.append(guess)
                if guess in word:
                    for x in range(0, len(word)):
                        if word[x] == guess:
                            guesses[x] = guess
                    print_word_to_guess(guesses)

                    if not "_ " in guesses:
                        print("You won!", "The word was:", word)
                        break
                else:
                    max_fails -= 1
                    print(max_fails, "mistakes left")

                    if max_fails == 0:
                        print("You lost :( The word was", word)

暂无
暂无

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

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