子手中的嵌套While循环

时间:2018-08-26 15:57:07

标签: python while-loop

我正在创建一个子手游戏。这里是条件

  • 用户将有六次选择错误的机会
  • 如果已输入字母,系统将通知用户该字母已存在(不会因输入两次错误而受到处罚)

现在我的问题是:

  • 如果要猜错的字母数达到7并且存在循环,但没有发生,我想显示“用户输掉游戏”的消息

这是我的代码:

print("Welcome to Hangman"
      "__________________")
word = "Python"
wordlist=list(word)
wordlist.sort()

print(wordlist)
print("Word's length is", len(word))
letter=" "
used_letter=[]
bad_letter=[]
guess=[]
tries=0
while len(bad_letter)<7:

    while guess != wordlist or letter !="exit":
        letter = input("Guess your letter:")
        if letter in word and letter not in used_letter:
            guess.append(letter)
            print("good guess")
        elif letter in used_letter:
            print("letter already used")
        else:
            bad_letter.append(letter)
            print("bad guess")
        used_letter.append(letter)
        tries+=1
        print("You have ",6 - len(bad_letter), " tries remaining")
        print(guess)
        print("You have made ",tries," tries so far")
        guess.sort()

    print("Thank you for playing the game, you guessed in ",tries," tries.")
    print("Your guessed word is", word)
print("you lost the game")

我对python很陌生,所以我希望能对基本概念有所帮助

2 个答案:

答案 0 :(得分:0)

您的while len(bad_letter) < loop似乎永远不会退出,因为它下面的while循环会一直检查正确答案或退出条目永远运行。

您应该做的是只有一个master while循环,该循环每次都接收一个新输入,然后检查该输入是否符合您要查找的任何条件。

您可以这样构造它:

bad_letter = []
tries = 0
found_word = False

while len(bad_letter) < 7:
    letter = input("Guess your letter:")
    if letter == 'exit':
        break  # Exit game loop

    if letter in word and letter not in used_letter:
        guess.append(letter)
        print("good guess")
    elif letter in used_letter:
        print("letter already used")
    else:
        bad_letter.append(letter)
        print("bad guess")

    used_letter.append(letter)
    tries+=1
    print("You have ", 6 - len(bad_letter), " tries remaining")
    print(guess)
    print("You have made ",tries," tries so far")
    guess.sort()

    if guess == wordlist:
        found_word = True
        break  # Exits the while loop

print("Thankyou for playing tha game, you guessed in ",tries," tries.")
print("Your guessed word is", word)
if found_word:
    print("You won")
else:
    print("you lost the game")

答案 1 :(得分:0)

解决方案

word = 'Zam'
guesses = []
guesses_string = ''
bad_guesses = []
chances = 6

def one_letter(message=""):
    user_input = 'more than one letter'
    while len(user_input) > 1 or user_input == '':
        user_input = input(message)
    return user_input


while chances > 0:
    if sorted(word.lower()) == sorted(guesses_string.lower()):
        print("\nYou guessed every letter! (" + guesses_string + ")")
        break
    guess = one_letter("\nGuess a letter: ")
    if guess in guesses or guess in bad_guesses:
        print("You already guessed the letter " + guess + ".")
        continue
    elif guess.lower() in word.lower():
        print("Good guess " + guess + " is in the word!.")
        guesses.append(guess)
        guesses_string = ''.join(guesses)
        print("Letters guessed: " + guesses_string)
    else:
        print("Sorry, " + guess + " is not in the word.")
        bad_guesses.append(guess)
        chances -= 1
        print(str(chances) + " chances remaning.")

if chances == 0:
    print("\nGame Over, You Lose.")
else:
    word_guess = ''
    while word_guess.lower() != word.lower():
        word_guess = input("Guess the word: ('quit' to give up) ")    
        if word_guess.lower() == 'quit':
           print("You gave up, the word was " + word.title() + "!")
           break
    if word_guess.lower() == word.lower():
        print("\nCongratulations YOU WON! The word was "
            + word.title() + "!")

嘿!我是编程的新手(第二周),并不断看到这些“ hang子手”游戏的弹出窗口,因为今天是我的学习休息点,也是您试图实现并实现目标的目标。 在此处添加了一些额外的功能,例如,如果所有字母都被猜出,则打破循环,然后要求用户猜出最后一个单词。

注释

您可以使用此功能进行大量的重构(几乎让我很不高兴发布此原始表格),但我只是在旅途中脱口而出。 还要注意,我的最初目标是坚持不让您输入相同字母两次的问题。因此,此解决方案仅适用于不包含重复字母的单词。

希望这会有所帮助!当我有时间时,我将对其进行重构和更新,祝您好运!

更新 添加的bad_guesses = []可以阻止两次输入错误的猜测。