Python-递加计数器

计数本:number.txt

1
2
3
4

主程序:计数器

 # Author: Stephen Yuan
# 递加计算器
import os # 递加计算器
def calc():
file_size = os.path.getsize('number.txt')
count = 1
if file_size == 0:
while True:
with open('number.txt', 'a', encoding='utf-8') as f:
f.write(str(count) + '\n')
print(count)
count += 1
if_choice()
elif file_size != 0:
while True:
with open('number.txt', 'r+', encoding='utf-8') as f:
lines = f.readlines()
last_line = int(lines[-1]) # 获取文件的最后一行内容
print(last_line)
last_line += 1
f.write(str(last_line) + '\n')
if_choice() def if_choice():
info = input('请选择:')
if info == 'a':
calc() elif info == 'r':
with open('number.txt.', 'w', encoding='utf-8') as f:
pass
calc()
elif info == 'q':
exit() def choice():
choice = '''
1.计数,请按‘a’
2.重置,请按‘r’
3.退出,请按‘q’
'''.strip()
print(choice)
info = input('\n请选择:')
if info == 'a':
calc()
elif info == 'r':
with open('number.txt.', 'w', encoding='utf-8') as f:
pass
calc()
else:
exit() choice()

 

上一篇:CSRF(跨站请求伪造攻击)漏洞详解


下一篇:上次被人说TK不好咯,这次给你整个高大上的