程序没有关闭

扎眼的阳光 python 410

原文标题Program doesn’t close

我是一个 python 初学者,最近,我正在尝试编写一个石头剪刀布游戏。我试图询问玩家是否想结束游戏。然而,当玩家输入“真”或“假”时,游戏什么也不做,并再次重复。我该如何解决?

import random
import sys

playing = "True"
chosen_symbol = "nothing"
player_symbol = "nothing"
answer = "nothing"


def ai_responses():
    global answer
    answer = random.choice(("You lost! Haha!", "You lost! Imagine getting beaten by an AI.", "You lost! Kinda bad.",
                            "You lost. Maybe you'll beat me next time.", "Ur trash."))
    return


def ai_responses2():
    global answer
    answer = random.choice(("You won.", "How did you win? Good job.", "You won! Great job.",
                            "You beat me. Nice job.", "Noooooooooooooo... You won."))
    return


print("We are going to play Rock, Paper, Scissors. Get ready!")

while playing:
    global playing
    chosen_symbol = random.choice(("Rock", "Paper", "Scissors"))
    player_symbol = (input("Enter your choice."))
    print(f"I choose... {chosen_symbol}")
    if chosen_symbol == player_symbol:
        print("Tie!")
    elif chosen_symbol == "Paper" and player_symbol == "Rock":
        ai_responses()
        print(answer)
    elif chosen_symbol == "Paper" and player_symbol == "Scissors":
        ai_responses2()
        print(answer)
    elif chosen_symbol == "Rock" and player_symbol == "Scissors":
        ai_responses()
        print(answer)
    elif chosen_symbol == "Rock" and player_symbol == "Paper":
        ai_responses2()
        print(answer)
    elif chosen_symbol == "Scissors" and player_symbol == "Paper":
        ai_responses()
        print(answer)
    elif chosen_symbol == "Scissors" and player_symbol == "Rock":
        ai_responses2()
        print(answer)
    playing = (input("Do you want to play again(True/False)?"))

if not playing:
    print("Good game.")
    raise sys.exit()

我把问题归结为

    playing = (input("Do you want to play again(True/False)?")) 

部分。我希望当玩家输入“False”时程序会关​​闭。

原文链接:https://stackoverflow.com//questions/71686255/program-doesnt-close

回复

我来回复
  • Rayan Hatout的头像
    Rayan Hatout 评论

    问题在于以下检查:

    if not playing:
    

    playing在你的代码中是一个字符串,一个字符串的真值是False如果字符串是空的(例如""),True否则。

    由于您总是在其中存储一个非空字符串,因此检查将始终为True

    要解决此问题,请将playing声明为文字布尔值:

    playing = True
    

    在查询用户时,对他们输入的内容进行额外检查并相应地设置变量:

    user_input = (input("Do you want to play again(True/False)?"))
    if user_input == "False"
        playing = False
    else:
        # You can add some error handling here if the user 
        # doesn't type one of the given choices
    
    2年前 0条评论
  • CN-LanBao的头像
    CN-LanBao 评论

    我认为当用户输入“True”时,游戏将退出。将默认值设置为其他喜欢playing = "False"。并将while语句固定为while "False" == playing
    如果字符串不为空,则为 True!
    最后修改exit语句

    if "True" == playing:
        print("Good game.")
        exit()
    
    2年前 0条评论
  • Blue Robin的头像
    Blue Robin 评论

    将 if 语句放在 while 循环中(这非常重要,其他答案没有解决这个问题),你可以用这个检查字符串:

    playing = input("Do you want to play again(True/False)?")
    print(playing.lower())
    if playing.lower() == "false":
        print("Good game.")
        sys.exit()
    

    这将检查字符串的较低值并检查它是真还是假。

    The output will be:
    We are going to play Rock, Paper, Scissors. Get ready!
    Enter your choice.false
    I choose... Rock
    Do you want to play again(True/False)?false
    false
    Good game.
    
    2年前 0条评论
  • renaldyks的头像
    renaldyks 评论

    我认为,您的代码有两个问题。

    首先,您将playing声明为 String 而不是布尔值。

    其次,playing = (input("Do you want to play again(True/False)?"))作为字符串不是布尔值。

    我建议你像这样编辑你的playing变量

    playing = True
    

    您可以在您的代码中删除global playing。最后,您可以将此代码放入您的while代码中,

        playing=input("Do you want to play again(True/False)?")
        if playing == "False":
            playing = False
            print("break")
            break
    

    我已经测试过了。祝你好运!!!

    输出:

    We are going to play Rock, Paper, Scissors. Get ready!
    Enter your choice.Rock
    I choose... Scissors
    You won! Great job.
    Do you want to play again(True/False)?True
    Enter your choice.Paper
    I choose... Paper
    Tie!
    Do you want to play again(True/False)?False
    break
    Good game.
    
    2年前 0条评论