如何在单词完成后检测刽子手获胜

我很欣赏我的代码很混乱,但它可以工作,在大多数情况下,它只是无法检测到我何时获胜。我尝试制作Word_chooosed 一根绳子,希望能有所帮助,但没有。


我已经尝试了我的工具包中的所有内容;接下来我可以尝试什么?


输入:


import random

Words = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]

Word_choosed = random.choice(Words)

Word_Choosed = list(Word_choosed)

a =[]

print(Word_Choosed)

for i in Word_choosed:

    a += "_"

print(" ".join(a))


wrong = 0

while wrong < 5:

    Incorrect = "yes"

    Guess = str(input("Guess a letter?\n").upper())

    for ltr in range(0, len(Word_choosed)):

        if Guess == Word_choosed[ltr]:


            a[ltr] = Guess

            print(a)

            wrong -= 1

            Incorrect = "no"


    if Guess != Word_choosed:

        wrong += 1

        if Incorrect == "yes":

                print(f"wrong you have {5 - wrong } chances left")

        elif wrong == 5:

                print("you lost")

    elif a == Word_Choosed:

        print("you won")

        break

输出:


['P', 'E', 'S', 'I', 'D', 'O', 'N']

_ _ _ _ _ _ _

Guess a letter?

p

['P', '_', '_', '_', '_', '_', '_']

Guess a letter?

e

['P', 'E', '_', '_', '_', '_', '_']

Guess a letter?

s

['P', 'E', 'S', '_', '_', '_', '_']

Guess a letter?

i

['P', 'E', 'S', 'I', '_', '_', '_']

Guess a letter?

d

['P', 'E', 'S', 'I', 'D', '_', '_']

Guess a letter?

o

['P', 'E', 'S', 'I', 'D', 'O', '_']

Guess a letter?

n

['P', 'E', 'S', 'I', 'D', 'O', 'N']

Guess a letter?

n

['P', 'E', 'S', 'I', 'D', 'O', 'N']

Guess a letter?


胡说叔叔
浏览 85回答 2
2回答

繁星点点滴滴

稍微清理一下,就可以正常使用了!import randomWords = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]Word_choosed = random.choice(Words)Word_Choosed = list(Word_choosed)a =[]print(Word_Choosed)for i in Word_choosed:&nbsp; &nbsp; a += "_"wrong = 0correct = 0while correct < 5:&nbsp; &nbsp; print(''.join(a))&nbsp; &nbsp; Incorrect = "yes"&nbsp; &nbsp; Guess = str(input("Guess a letter?\n").upper())&nbsp; &nbsp; for ltr in range(0, len(Word_choosed)):&nbsp; &nbsp; &nbsp; &nbsp; if Guess == Word_choosed[ltr]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[ltr] = Guess&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Incorrect = "no"&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if Incorrect == "yes":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wrong += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"wrong you have {5 - wrong } chances left")&nbsp; &nbsp; if 5 - wrong == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you lost")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; elif a == Word_Choosed:&nbsp; &nbsp; &nbsp; &nbsp; print("you won")&nbsp; &nbsp; &nbsp; &nbsp; break

牛魔王的故事

您elif:在获胜和失败的代码中添加了一条语句。因此,python 只会执行一个 if 并且不会继续执行 elif 语句。你必须像这样修复你的代码:import randomWords = ["Hades".upper(),"Zues".upper(), "pesidon".upper()]Word_choosed = random.choice(Words)Word_Choosed = list(Word_choosed)a =[]print(Word_Choosed)for i in Word_choosed:&nbsp; &nbsp; a += "_"print(" ".join(a))wrong = 0while wrong < 5:&nbsp; &nbsp; Incorrect = "yes"&nbsp; &nbsp; Guess = str(input("Guess a letter?\n").upper())&nbsp; &nbsp; for ltr in range(len(Word_choosed)):&nbsp; &nbsp; &nbsp; &nbsp; if Guess == Word_choosed[ltr]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[ltr] = Guess&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wrong -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Incorrect = "no"&nbsp; &nbsp; if Guess != Word_choosed:&nbsp; &nbsp; &nbsp; &nbsp; wrong += 1&nbsp; &nbsp; &nbsp; &nbsp; if Incorrect == "yes":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"wrong you have {5 - wrong } chances left")&nbsp; &nbsp; &nbsp; &nbsp; if wrong == 5: # I changed this to if&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("you lost")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break # I added break you can remove it if you like&nbsp; &nbsp; if list(a) == list(Word_Choosed): # changed this to if&nbsp; &nbsp; &nbsp; &nbsp; print("you won")&nbsp; &nbsp; &nbsp; &nbsp; break
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python