当玩家猜出所有字母时,Python Hangman没有结束

时间:2019-05-27 08:19:55

标签: python

我的问题是,当我运行该程序并使所有字母正确时,它不会从那里继续前进,并且我处于无限循环中。我希望它说“好工作!”并在播放器正确输入单词后结束程序。我对编码非常陌生,将不胜感激。

import random
import time

name = input("What is your name? ")
print(name + ", ay?")
time.sleep(1)
start = input("Up for a game of Hangman?(y/n) ")

lis = random.choice(["yet"])
dash = []
while len(dash) != len(lis):
   dash.append("_") 
guess = []
guesscomb = "".join(guess)
wrongcount=int(0)
alphabet = "abcdefghijklmnopqrstuvwxyz"

if start == "y":
    print("One game of Hangman comin' right up,",name)
    letter = input("Alright then, Guess a letter: ")
    thing = ''.join(dash)

    while guesscomb != thing:
        if letter == "" or letter == " " or len(letter) != 1:
            print("I don't understand. Please only use singular letters.")
            letter = input("Guess a letter: ")    

        elif letter in lis and letter in alphabet:
            print("Nice!")

            location = lis.find(letter)
            dash[location] = letter
            guess.append(letter)
            alphabet.replace(letter," ")
            guesscomb = "".join(guess)

            letter = input("Guess a letter: ")

        else:
            print("Wrong.")
            wrongcount = wrongcount + 1

            print("Total Mistakes:",wrongcount)
            letter = input("Guess a letter: ")      

elif start == "n":
    input("Shame.")
    quit()

print("Good Job!")
time.sleep(10)

2 个答案:

答案 0 :(得分:0)

thing变量等于___,而lis始终等于"yet"

guesscomb不能等于thing,因为当猜测等于lis中的字母时您验证字母

答案 1 :(得分:0)

您可以将print与参数end=""一起使用,以使光标不会移到新行。 您可以在字符串上使用方法isalpha来检查是否全部为字符,而不是将其与字母进行比较。 正如本所说,thing始终是___

修改代码的这一部分,它将起作用

if start == "y":
    print("One game of Hangman comin' right up,", name)
    print("Alright then, ", end="")
    # letter = input("Alright then, Guess a letter: ")
    thing = ''.join(dash)

    while guesscomb != thing:
        letter = input("Guess a letter: ")
        if letter == "" or letter == " " or len(letter) != 1:
            print("I don't understand. Please only use singular letters.")

        elif letter in lis and letter in alphabet:
            print("Nice!")
            location = lis.find(letter)
            dash[location] = letter
            guess.append(letter)
            alphabet.replace(letter, " ")
            guesscomb = "".join(guess)
        else:
            print("Wrong.")
            wrongcount = wrongcount + 1
            print("Total Mistakes:", wrongcount)
        thing = ''.join(dash)