为了账号安全,请及时绑定邮箱和手机立即绑定

python 简单计数器的实现

python 简单计数器的实现

UYOU 2019-05-23 17:38:52
各位大佬好,我想要写一个计数器,每次一个循环后,计数器数量加一,但不知道怎么实现。麻烦各位赐教defcount_turn(turn=0):ifturn==0:turn+=1elifturn>=1:turn+=1returnturndefadd_turn(turn):turn+=1returnturndefleave_stay():print("="*50)reply=input("Doyouwanttoquitthegameorcontinueplaying?""\n'q'forquit,'c'forcontinue")print("")returnreplydefexecute(reply,):ifreply=='q':signal=Falsereturnsignalelifreply=='c':main()else:leave_stay()defmain(turn=0):turn=count_turn(turn)print("first:",turn)signal=Trueifsignal:turn=count_turn(turn)turn=add_turn(turn)print("hereistheturn:",turn)reply=leave_stay()execute(reply)main()
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

classCounter(object):
def__init__(self,start=0):
self.num=start
defcount(self):
self.num+=1
returnself.num
调用如下:
>>>c=Counter()
>>>c.count()
1
>>>c.count()
2
                            
查看完整回答
反对 回复 2019-05-23
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

defexecute(reply,):
whilereplynotin'qc':
reply=leave_stay()
returnTrueifreply=='c'elseFalse
defmain(turn=0):
turn=count_turn(turn)
print("first:",turn)
signal=True
whilesignal:
turn=count_turn(turn)
print("hereistheturn:",turn)
reply=leave_stay()
signal=execute(reply)
main()
                            
查看完整回答
反对 回复 2019-05-23
  • 2 回答
  • 0 关注
  • 2976 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信