询问用户他/她是否想再玩一次但输入是只是再次重复这个问题而不是重新开始

这是一个数字猜谜游戏


解释


首先它要求用户输入一个介于 1 到 50 之间的数字然后如果数字是正确的那么你赢了否则你必须再试一次(中奖号码是随机的)你也有有限的猜测


问题在代码下面提到 这是我的代码:)


import random

winning_num = 23

guesses = 1

guesses_left = 9

game_over = False

end_game = False

number_enter = False

while not end_game:    

    while not number_enter:

        try:

            ask = int(input("ENTER A NUMBER BETWEEN 1 AND 50: "))

            print(f"TOTAL GUESSES = {guesses_left}")

            break

        except ValueError:

            print("INVALID INPUT!!")

            continue

    while not game_over: 

        if ask==winning_num:

            print(f"YOU WON BY GUESSING THE NUMBER IN {guesses} TIME(S)!!")

            print("DO YOU WANT TO PLAY AGAIN?")

            while True:

                ask1 = input("ENTER 'YES' OR 'NO' ONLY: ")

                ask1 = ask1.lower()

                if ask1=='yes':

                    print("YOU CHOSE TO PLAY AGAIN")

                    game_over = False

                    break

                elif ask1=="no":

                    print("THANK YOU FOR PLAYING THIS GAME")

                    game_over = True

                    end_game = True

                    break

                else:

                    print("PLEASE WRITE 'YES' OR 'NO' ONLY ")

                    continue

            


        elif ask>winning_num:

            print("TOO HIGH!!")

            guesses+=1

            guesses_left-=1

            while True:

                try:

                    ask = int(input("TRY AGAIN: "))

                    print(f"GUESSES LEFT = {guesses_left}")

                    break

                except ValueError:

                    print("INVALID INPUT!!")

                    continue

            if guesses_left==1:

                print("ONLY ONE GUESS LEFT!!")

                continue

            elif guesses_left==0:

                print("YOU LOSE!!")

                break

    

问题是游戏何时结束它询问我们是否要再次玩但是如果我们再次键入“是”它会询问相同的“你想再次玩吗”但是键入“否”可以正常运行并且程序结束


交互式爱情
浏览 100回答 3
3回答

慕村225694

你必须设置 game_over = False 以防 ask1 = yes 以便它可以从父 while 循环中出来并继续。此外,您必须重新设置猜测次数等,以便它作为新游戏开始。

慕桂英4014372

import randomwinning_num = 23guesses = 1guesses_left = 9game_over = Falseend_game = Falsenumber_enter = Falsewhile not end_game:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while not number_enter:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask = int(input("ENTER A NUMBER BETWEEN 1 AND 50: "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"TOTAL GUESSES = {guesses_left}")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("INVALID INPUT!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; while not game_over:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ask==winning_num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"YOU WON BY GUESSING THE NUMBER IN {guesses} TIME(S)!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("DO YOU WANT TO PLAY AGAIN?")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask1 = input("ENTER 'YES' OR 'NO' ONLY: ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask1 = ask1.lower()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ask1=='yes':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("YOU CHOSE TO PLAY AGAIN")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game_over = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif ask1=="no":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("THANK YOU FOR PLAYING THIS GAME")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game_over = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end_game = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("PLEASE WRITE 'YES' OR 'NO' ONLY ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; elif ask>winning_num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("TOO HIGH!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guesses+=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guesses_left-=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask = int(input("TRY AGAIN: "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(f"GUESSES LEFT = {guesses_left}")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("INVALID INPUT!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if guesses_left==1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("ONLY ONE GUESS LEFT!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif guesses_left==0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("YOU LOSE!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif ask<winning_num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("TOO LOW!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guesses+=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guesses_left-=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ask = int(input("TRY AGAIN: "))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(f"GUESSES LEFT = {guesses_left}")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("INVALID INPUT!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if guesses_left==1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("ONLY ONE GUESS LEFT!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif guesses_left==0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("YOU LOSE!!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break

翻翻过去那场雪

您错误地切换了 game_over,它应该设置为True,而不是False如果重播的答案是肯定的。while not end_game:&nbsp; # End game must be false to replay&nbsp; &nbsp; while not number_enter:&nbsp; &nbsp; &nbsp; &nbsp; #... ask number&nbsp; &nbsp; while not game_over: # But Game_over should be True to stop asking to replay&nbsp; &nbsp; &nbsp; &nbsp; #... Check number good&nbsp; &nbsp; &nbsp; &nbsp; #... Ask to replay&nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask1 = input("ENTER 'YES' OR 'NO' ONLY: ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ask1 = ask1.lower()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ask1=='yes':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("YOU CHOSE TO PLAY AGAIN")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game_over = True&nbsp; # <<<< Thats the problematic part, it must be True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; in your code it is False, So it result in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; an "infinite" loop, if yes.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python