python 简单计数器的实现

新手上路,请多包涵

各位大佬好, 我想要写一个计数器,每次一个循环后,计数器数量加一,但不知道怎么实现。麻烦各位赐教

def count_turn(turn=0):

if turn == 0:
    turn += 1
elif turn >=1:
    turn += 1
return turn

def add_turn(turn):

turn += 1
return turn

def leave_stay():

print("="*50)
reply = input("Do you want to quit the game or continue playing? "
              "\n'q' for quit, 'c' for continue ")
print("")
return reply

def execute(reply,):

if reply == 'q':
    signal = False
    return  signal
elif reply == 'c':
    main()
else:
    leave_stay()

def main(turn =0):

turn = count_turn(turn)
print("first:", turn)
signal = True
if signal:
    turn = count_turn(turn)
    turn=add_turn(turn)
    print("here is the turn :", turn)
    reply = leave_stay()
    execute(reply)

main()

阅读 11.6k
2 个回答
class Counter(object):
    def __init__(self, start=0):
        self.num = start
    def count(self):
        self.num += 1
        return self.num

调用如下:

>>> c = Counter()
>>> c.count()
1
>>> c.count()
2
def execute(reply,):
    while reply not in 'qc':
        reply = leave_stay()
    return True if reply=='c' else False

def main(turn=0):
    turn = count_turn(turn)
    print("first:", turn)
    signal = True
    while signal:
        turn = count_turn(turn)
        print("here is the turn :", turn)
        reply = leave_stay()
        signal = execute(reply)

main()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题