简体   繁体   English

如何使用用户输入将程序循环回到开头

[英]How to loop the program back to the beginning with user input

I'm setting up a basic hangman game and am trying to get the program to loop back to the beginning when the game is finished. 我正在设置一个基本的子手游戏,并试图让该程序在游戏结束时循环回到开始。

    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            print("Play Again? (y/n)")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                print("Play again? (y/n)")

Wrap your game in a function and throw it in a while loop. 将您的游戏包装到一个函数中,并将其放入while循环中。 After the play game function, they'll be asked to play again. 玩游戏功能后,将要求他们再次玩。 If they respond with anything but 'y', the loop breaks. 如果他们回答“ y”以外的任何内容,则循环中断。

while True:
    # play_game()
    if input("Play again?") != 'y':
        break
print("Thanks for playing!")

You can use an user input and a while loop 您可以使用用户输入和while循环

play_again = "Y"
while play_again == "Y" or play_again == "y":
    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            print("Play Again? (y/n)")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                play_again = input("Play again? (Y/N)")

or in short just put in in a function: 或简而言之,只是放入一个函数中:

play_again = "Y"
while play_again == "Y" or play_again == "y":
    game()
    play_again = input("Play again? (Y/N)")

You could put it all into a function and have it loop back like this: 您可以将所有内容放到一个函数中,然后像这样循环返回:

def start():
    replay = True
    while (replay):
        game_started()
        inp = input("Play again? Y/n ")
        if inp == 'n' or inp == 'N':
            replay = False

def game_started():
    print("Welcome to Hangman")
    print("Start guessing")

    word = "hangman"
    guesses = ''
    turns = 10

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print (char),
            else:
                print("_"),
                failed += 1
        if failed == 0:
            print("You won")
            break
        guess = input("Guess a character:")
        guesses += guess
        if guess not in word:
            turns -= 1
            print("wrong")
            print("You have", + turns, "more guesses")
            if turns == 0:
                print("You Lose")
                break

start()

Edit: Your check if a letter was guesses is also flawed: If you guess "abcdefghijklmnopqrstuvwxyz", you always win. 编辑:如果检查是否是字母猜测也有缺陷:如果您猜测“ abcdefghijklmnopqrstuvwxyz”,您将始终获胜。 I'd suggest checking the length of the input before appending it to guesses . 我建议在将输入追加到guesses之前检查输入的长度。 Also, to print it all into one line, you could use "print(char, end='')" (and "print('_', end='')", respectively). 另外,要将它们全部打印到一行,可以使用“ print(char,end ='')”(和分别使用“ print('_',end ='')”)。 Just make sure to print a newline after the loop, to finish the line. 只需确保在循环后打印换行符即可完成该行。

Hey friend here is what I came up with I hope it helps! 嘿,这是我想出的朋友,希望对您有所帮助! Instead of using your turns to control the while loop you can use a "running" variable that is set to a boolean which you can use input statements for your replay feature to control when you want to exit (False) or continue through your loop (True). 可以使用设置为布尔值的“运行”变量来代替使用轮流来控制while循环,您可以将输入语句用于重播功能来控制何时要退出(False)或继续执行循环(真正)。

print("Welcome to Hangman")
print("Start guessing")

word = "hangman"
guesses = ''
turns = 10
running = True

while running:

    failed = 0
    for char in word:
        if char in guesses:
            print(char),
        else:
            print("_"),
            failed += 1

    if failed <= 0:
        print("You won")
        x = input("Play Again? (y/n) \n")
        if x == "y":
            turns = 10
        else:
            running = False

    guess = input("Guess a character: \n")
    guesses += guess

    if guess not in word:
        turns -= 1
        print("wrong")
        print("You have", + turns, "more guesses")

        if turns == 0:
            print("You Lose")
            z = input("Play Again? (y/n) \n")
            if z == "y":
                turns = 10
            else:
                running = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM