为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

胡说叔叔 2023-10-05 17:01:05
我很欣赏我的代码很混乱,但它可以工作,在大多数情况下,它只是无法检测到我何时获胜。我尝试制作Word_chooosed 一根绳子,希望能有所帮助,但没有。我已经尝试了我的工具包中的所有内容;接下来我可以尝试什么?输入: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:    a += "_"print(" ".join(a))wrong = 0while 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?
查看完整描述

2 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

稍微清理一下,就可以正常使用了!


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 += "_"


wrong = 0

correct = 0

while correct < 5:

    print(''.join(a))

    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

            correct += 1

            Incorrect = "no"

    

    if Incorrect == "yes":

            wrong += 1

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

    if 5 - wrong == 0:

            print("you lost")

            break

    elif a == Word_Choosed:

        print("you won")

        break


查看完整回答
反对 回复 2023-10-05
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

您elif:在获胜和失败的代码中添加了一条语句。因此,python 只会执行一个 if 并且不会继续执行 elif 语句。你必须像这样修复你的代码:


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

        if wrong == 5: # I changed this to if

                print("you lost") 

                break # I added break you can remove it if you like

    if list(a) == list(Word_Choosed): # changed this to if

        print("you won")

        break


查看完整回答
反对 回复 2023-10-05
  • 2 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信