簡體   English   中英

達到分數限制時程序不會終止嗎?

[英]Program not terminating when score limit is reached?

我正在開發一個簡單的基於文本的瑣事游戲,這是我的第一個python項目,並且一旦達到得分限制,我的程序就不會終止。

def game(quest_list):
    points = 0
    score_limit = 20

    x, y = info()
    time.sleep(2)

    if y >= 18 and y < 100:
        time.sleep(1)
        while points < score_limit:
            random.choice(quest_list)(points)
            time.sleep(2)
            print("Current score:", points, "points")
        print("You beat the game!")
        quit()
    ...

看來points變量沒有增加。 這樣的事情可能會在您的內部循環中起作用:

    while points < score_limit:
        points = random.choice(quest_list)(points)
        time.sleep(2)
        print("Current score:", points, "points")

我假設quest_list是函數列表,並且您要將points值作為參數傳遞? 為了使該示例正常工作,您還需要從調用的quest_list返回的函數中返回點。 建立此方法的一種可能更干凈的方法是僅返回任務生成的點。 然后,您可以執行以下操作:

        quest = random.choice(quest_list)
        points += quest()

除非points是一個可變的數據結構,否則它不會更改值。 您可以在此StackOverflow問題中了解更多有關此內容的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM