import random import time import csv import os from datetime import datetime users={} for i in range(4): users_id=random.randint(0,10) users_score=random.randint(-8000,8000) users[users_id]=users_score with open('updates.csv','a')as f: csv_re=csv.writer(f) csv_re.writerow([users_id,users_score]) print(f'积分变动:{users_id} {users_score}') def aaa(): global users_id global users_score with open('updates.csv','r')as f: csv_re=csv.reader(f) for row in csv_re: users_id,users_score=row users_id=int(users_id) users_score=int(users_score) users[users_id]+=users_score if users[users_id]<0: users[users_id]=0 return users def bbb(): with open('Candidates.csv','w')as f: csv_re=csv.writer(f) csv_re.writerow([users_id,users_score]) def ccc(): global prize_winner weight=[] prize_winner=[] for uid,users_score in users.items(): if users_score >=3000: weight.append(3) elif users_score >=2000: weight.append(2) elif users_score >=1000: weight.append(1) else: weight.append(0) winner1=random.choices(list(users.keys()),weight) prize_winner.append(winner1[0]) print(f'一等奖:{prize_winner[0]}') def ddd(): winner2 = random.sample(list(users.keys()),2) prize_winner.append(winner2[0][1]) print(f'二等奖:{prize_winner[1]}') del users[prize_winner[1]] def timer(): nowtime=datetime.now() while True: if nowtime.weekday()==2 and nowtime.hour==22 and 0<=nowtime.minute<=60: return True else: return False for i in range(3): while not timer(): time.sleep(60) print(f'第{i+1}轮抽奖开始:') aaa() bbb() ccc() ddd() time.sleep(12) today_date_str=datetime.now().strftime('%Y_%m_%d') os.rename('updates.csv','{}.csv'.format(today_date_str))修改此段代码并且写出新代码
时间: 2024-03-14 21:48:06 浏览: 119
python中时间转换datetime和pd.to_datetime详析
修改建议:
1. 代码缩进不规范,需要统一缩进方式;
2. 函数名不够清晰,需要更改为更能表示其功能的名称;
3. 函数之间没有明确的调用关系,需要添加调用关系。
新代码如下:
```python
import random
import time
import csv
import os
from datetime import datetime
# 初始化用户积分信息
users = {}
for i in range(4):
users_id = random.randint(0, 10)
users_score = random.randint(-8000, 8000)
users[users_id] = users_score
with open('updates.csv', 'a') as f:
csv_re = csv.writer(f)
csv_re.writerow([users_id, users_score])
print(f'积分变动:{users_id} {users_score}')
def update_scores():
global users_id
global users_score
with open('updates.csv', 'r') as f:
csv_re = csv.reader(f)
for row in csv_re:
users_id, users_score = row
users_id = int(users_id)
users_score = int(users_score)
users[users_id] += users_score
if users[users_id] < 0:
users[users_id] = 0
return users
def write_candidates():
with open('Candidates.csv', 'w') as f:
csv_re = csv.writer(f)
for uid, users_score in users.items():
csv_re.writerow([uid, users_score])
def draw_first_prize():
weight = []
for uid, users_score in users.items():
if users_score >= 3000:
weight.append(3)
elif users_score >= 2000:
weight.append(2)
elif users_score >= 1000:
weight.append(1)
else:
weight.append(0)
winner1 = random.choices(list(users.keys()), weight)
prize_winner = [winner1[0]]
print(f'一等奖:{prize_winner[0]}')
return prize_winner
def draw_second_prize(prize_winner):
winner2 = random.sample(list(users.keys()), 2)
prize_winner.append(winner2[0])
print(f'二等奖:{prize_winner[1]}')
del users[prize_winner[1]]
def is_draw_time():
nowtime = datetime.now()
if nowtime.weekday() == 2 and nowtime.hour == 22 and 0 <= nowtime.minute <= 60:
return True
else:
return False
for i in range(3):
while not is_draw_time():
time.sleep(60)
print(f'第{i+1}轮抽奖开始:')
update_scores()
write_candidates()
prize_winner = draw_first_prize()
draw_second_prize(prize_winner)
time.sleep(12)
today_date_str = datetime.now().strftime('%Y_%m_%d')
os.rename('updates.csv', '{}.csv'.format(today_date_str))
```
新代码修改了函数名,添加了调用关系,使得代码更加清晰易懂。同时,修改了代码缩进方式,使得代码风格更加规范。
阅读全文