各位大佬好,我想要写一个计数器,每次一个循环后,计数器数量加一,但不知道怎么实现。麻烦各位赐教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=startdefcount(self):self.num+=1returnself.num调用如下:>>>c=Counter()>>>c.count()1>>>c.count()2
猛跑小猪
TA贡献1858条经验 获得超8个赞
defexecute(reply,):whilereplynotin'qc':reply=leave_stay()returnTrueifreply=='c'elseFalsedefmain(turn=0):turn=count_turn(turn)print("first:",turn)signal=Truewhilesignal:turn=count_turn(turn)print("hereistheturn:",turn)reply=leave_stay()signal=execute(reply)main()
添加回答
举报
0/150
提交
取消