简体   繁体   English

Python:猜词游戏

[英]Python: Word guessing game

I'm trying to make a word guessing game in Python, but the final part is a bit confusing to me. 我正在尝试用Python进行猜词游戏,但是最后一部分对我来说有些混乱。

Here is my code uptil now: 这是我直到现在的代码:

word_tuple = ("c", "o", "d", "e", "c")

word = ""

word = input("Give a word of " +  str(len(word_tuple)) + " characters: ")

while len(word) != len(word_tuple):
    if len(word) != len(word_tuple):
        print("Wrong!")
        word = input("Give a word of " + str(len(word_tuple)) + " characters: ")

for i in range(len(word_tuple)):
    print(word_tuple[i], end="")

Basically, the loop checks if you insert a 5 character word, and if you do, it compares the word with the characters of the tuple. 基本上,循环检查是否插入5个字符的单词,如果插入,则将单词与元组的字符进行比较。 If 1 or more characters are correct, it will print the correct characters, and the ones which haven't been guessed are masked with a symbol, for example '*'. 如果1个或多个字符正确,它将打印正确的字符,而没有被猜到的字符将被一个符号遮住,例如'*'。

The confusing part is where I have to check if the entered word has characters that match the tuple and then print out the correct characters. 令人困惑的部分是我必须检查输入的单词是否具有与元组匹配的字符,然后打印出正确的字符。

So for example: 因此,例如:

Give a word of 5 characters: Python
Wrong!
Give a word of 5 characters: Candy
Almost there! The word is "C*d*c"
Give a word of 5 characters: Denim
Almost there! The word is "C*dec"
Give a word of 5 characters: Codec
You found the word!

Any help would be greatly appreciated. 任何帮助将不胜感激。

Your problem is you do not print your word correctly, and your print is outside of the while, here is an answer you can try 您的问题是您的字词打印不正确,并且打印的时间不在此范围内,这是您可以尝试的答案

word_tuple = ("c", "o", "d", "e", "c")

# We use this list to keep in memory the letters found
found = [False] * len(word_tuple)

word = ""

# The `all` method return True only if the list contains only True values
# Which means, while all letters are not found

while not all(found):
    # the `lower` method allows you to not take in account the uppercases
    word = input("Give a word of " +  str(len(word_tuple)) + " characters: ").lower()

    if len(word) == len(word_tuple):
        for charac in word_tuple:
            if charac in word:
                found = [b or word_tuple[index] in word for index, b in enumerate(found)]
        # The `any` method return True only if the list contains at least one True value
        # Which means we print Wrong only if there is no letter found
        if not any(found):
            print('Wrong!')
        else:
            print('Almost there! The word is "', end='')
            for i in range(len(word_tuple)):
                if found[i]:
                    print(word_tuple[i], end="")
                else:
                    print('*', end='')
            print('"')

    else:
        print('Wrong!')
# The method `join` allows you to join every string of an iterable
# Which means it joins every character of your tuple to a printable string
while word != ''.join(word_tuple):
    print('Close, try again')
    word = input("Give a word of " +  str(len(word_tuple)) + " characters: ").lower()

print('You found the word!')

An exercise can be to refactor this code in different methods 一个练习可以是用不同的方法重构此代码

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

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