猿问

python 简单计数器的实现

各位大佬好,我想要写一个计数器,每次一个循环后,计数器数量加一,但不知道怎么实现。麻烦各位赐教
defcount_turn(turn=0):
ifturn==0:
turn+=1
elifturn>=1:
turn+=1
returnturn
defadd_turn(turn):
turn+=1
returnturn
defleave_stay():
print("="*50)
reply=input("Doyouwanttoquitthegameorcontinueplaying?"
"\n'q'forquit,'c'forcontinue")
print("")
returnreply
defexecute(reply,):
ifreply=='q':
signal=False
returnsignal
elifreply=='c':
main()
else:
leave_stay()
defmain(turn=0):
turn=count_turn(turn)
print("first:",turn)
signal=True
ifsignal:
turn=count_turn(turn)
turn=add_turn(turn)
print("hereistheturn:",turn)
reply=leave_stay()
execute(reply)
main()
UYOU
浏览 2977回答 2
2回答

梦里花落0921

classCounter(object):def__init__(self,start=0):self.num=startdefcount(self):self.num+=1returnself.num调用如下:>>>c=Counter()>>>c.count()1>>>c.count()2

猛跑小猪

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()
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答