简体   繁体   English

如何在我的程序中重置 A 'Score Bank'? Python

[英]How Do I Reset The A 'Score Bank' In My Program? Python

So I'm trying to add in a scoring system with an external module in a really basic game as I just started Python two days ago.因此,当我两天前刚开始 Python 时,我试图在一个非常基本的游戏中添加一个带有外部模块的评分系统。

score_history = [0]

a=cur_score()

def var1():
    scoremodule.cur_score()

def initial_score():
    return score[0]

def cur_score():
    return score_history[-1]

def affect_score(delta):
    score_history.append(cur_score() + delta)
    return cur_score()

def reset_score(var1):
    sub=a-a
    print(sub)

Here's what I have so far.这是我到目前为止所拥有的。 I just need a way to reset the score with the reset_score argument.我只需要一种使用reset_score参数重置分数的方法。 I can't figure out how to assign cur_score a letter or subtract it from its self.我不知道如何为cur_score分配一个字母或从其自身中减去它。 Help would be much appreciated.帮助将不胜感激。

what do you want reset_score to do?你想让 reset_score 做什么? set a = 0?设置 a = 0?

def reset_score(var1()):
    a=0

You could use a class to define your methods and then have the score history as an attribute of your class?您可以使用 class 来定义您的方法,然后将分数历史记录作为 class 的属性? Is this the result you are after?这是你追求的结果吗?

class Scorer:
    def __init__(self):
        self.score_history = [0]

    def initial_score(self):
        return self.score_history[0]

    def current_score(self):
        return self.score_history[-1]

    def affect_score(self, delta):
        self.score_history.append(self.score_history[-1] + delta)
        return self.current_score()

    def reset_score(self):
        self.score_history = [0]

scoreboard = Scorer()

print(f"Start: {scoreboard.current_score()}")
print(f"add 5: {scoreboard.affect_score(5)}")
print(f"initial: {scoreboard.initial_score()}")
print(f"current: {scoreboard.current_score()}")
scoreboard.reset_score()
print(f"current after reset: {scoreboard.current_score()}")

Output Output

Start: 0
add 5: 5
initial: 0
current: 5
current after reset: 0

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

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