簡體   English   中英

猜python中的文字游戲需要結束

[英]Guess word game in python needs to end

如何結束這場比賽? 如果整個單詞都猜對了,或者您失去了全部生命,但是如果有人一個個地猜出所有字母,我將無法結束。

secret_word = "something".upper()
lifes = u'\u2764'
total_lifes = 5
print("You have 5 lifes in total to guess the answer")
guess = None
for_list = "?"*len(secret_word)
my_list = list(for_list)

def change_user_output(value):
    for j in range(0, len(secret_word)):
        if (value == secret_word[j]):
            my_list[j] = value
    new_user_output = " ".join(my_list)
    print(new_user_output)

while total_lifes > 0:
    print(lifes * total_lifes)
    print("Guess any letter : ")
    guess = input().upper()
    if(guess == secret_word):
        print("WOW that is correct, the secret word is %s"%secret_word)
        break
    if(guess in secret_word):
        print("You guessed it right %s is in SECRET WORD"%guess)
        change_user_output(guess)
    else:
        print("There is no such letter in the SECRET WORD, you loose a life")
        change_user_output(guess)
        total_lifes -= 1

if total_lifes == 0:
    print("You've lost all lifes, GAME OVER FOR YOU")
    print("The SECRET WORD was : %s"%"".join(secret_word))

問題在於if (guess == secret_word):始終為False因為guess是單個字母,並且每次迭代guess = input().upper()都會被新的輸入替換。 因此,在正確猜測的情況下,您的循環將永遠不會退出,只有在您結束生命后才會退出。

您創建一組所需的所有字母。 您將每個猜出的字母添加到另一個集合中。 如果所需字母集是猜測字母集的子集,則完成:

secret_word = "apple".upper()
lifes = '#'
total_lifes = 5
print("You have 5 lifes in total to guess the answer")
guess = None
for_list = "?"*len(secret_word)
my_list = list(for_list)

characters_needed = set(secret_word)  # set of all letters needed
characters_guessed = set()            # set of guessed letters
len_secret = len(secret_word)

def change_user_output(value):
    for j in range(0, len(secret_word)):
        if (value == secret_word[j]):
            my_list[j] = value
    new_user_output = " ".join(my_list)
    print(new_user_output)

while total_lifes > 0:
    print(lifes * total_lifes)
    print("Guess any letter : ")
    guess = input().upper()

    # check for correct lenghts of input:
    if len(guess) not in (1,len_secret):
        print("1 letter or the whole word! Try again.")
        continue

    if(guess == secret_word):
        print("WOW that is correct, the secret word is {}".format(secret_word))
    elif(len(guess) > 1):
        print("Thats not the SECRET WORD.")
        total_lifes -= 1
        continue

    # if you input a whole word, this will decrease the lives  
    # as no multiletter-input can be in your set of characters
    if(guess in characters_needed): 
        print("You guessed it right {} is in SECRET WORD".format(guess))
        characters_guessed.add(guess)
        change_user_output(guess)
    else:
        print("There is no such letter in the SECRET WORD, you loose a life")
        change_user_output(guess)
        total_lifes -= 1

    if (characters_needed.issubset(characters_guessed)):
        print("You guessed all the characters. The secret word is {}".format(secret_word))
        break

if total_lifes == 0:
    print("You've lost all lifes, GAME OVER FOR YOU")
    print("The SECRET WORD was : %s"%"".join(secret_word))

輸出(對於a,p,l,e):

#####
Guess any letter : a
You guessed it right A is in SECRET WORD
A ? ? ? ?
#####
Guess any letter : p
You guessed it right P is in SECRET WORD
A P P ? ?
#####
Guess any letter : l
You guessed it right L is in SECRET WORD
A P P L ?
#####
Guess any letter : e
You guessed it right E is in SECRET WORD
A P P L E
You guessed all the characgters. The secret word is APPLE

我還添加了一些保護措施,以允許輸入整個單詞或1個字符。

您可以添加以下內容以檢查猜出的字母是否與密碼中的字母匹配:

if(guess in secret_word):
    print("You guessed it right %s is in SECRET WORD"%guess)
    change_user_output(guess)

    # New code below here.

    if set(list(secret_word)) == set(my_list):
        print("WOW that is correct, the secret word is %s"%secret_word)
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM