import random import time import csv 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(users): 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) print(f'一等奖:{prize_winner[0]}') def ddd(): weight=[] for uid,users_score in users.items(): if users_score>0: weight.append(1) else: weight.append(0) winner2=random.choices(list(users.keys()),weight) prize_winner.append(winner2) print(f'二等奖:{prize_winner[1]}') del users[prize_winner[1]] def timer(): nowtime=datetime.now() while True: if nowtime.weekday()==2 and nowtime.hour==21 and 0<=nowtime.minute<=60: return True return False for i in range(3): while not timer(): time.sleep(60) print(f'第{i+1}轮抽奖开始:') aaa(users) bbb() ccc() ddd() time.sleep(1200) today_date_str=datetime.now().strftime('%Y_%m_%d') os.rename('updates.csv','{}.csv'.format(today_date_str))找出代码中的问题并写出正确的代码
时间: 2024-03-14 17:46:08 浏览: 138
python中时间转换datetime和pd.to_datetime详析
代码中有以下问题:
1. 在第8行应该为 updates.csv,而不是 updates,csv。
2. 函数 aaa 中应该加入 global 声明,以便在函数内部修改全局变量 users。
3. 函数 bbb 中应该将 users_id 和 users_score 作为参数传入,或者在函数内部通过 users 获取相关信息。
4. 函数 ccc 中应该使用 winner1[0] 和 winner2[0] 获取获奖者的 ID,否则会将 ID 和 [] 都存入 prize_winner 中。
5. 函数 ddd 中删除获奖者时应该使用 del users[prize_winner[1]],而不是 del users[prize_winner]。
6. 函数 timer 中的 return False 应该放在 while 循环外,否则只会执行一次。
以下是修改后的代码:
```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 aaa():
global users
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(users):
with open('Candidates.csv', 'w') as f:
csv_re = csv.writer(f)
for users_id, users_score in users.items():
csv_re.writerow([users_id, users_score])
def ccc(users):
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(users):
weight = []
for uid, users_score in users.items():
if users_score > 0:
weight.append(1)
else:
weight.append(0)
winner2 = random.choices(list(users.keys()), weight)
prize_winner.append(winner2[0])
print(f'二等奖:{prize_winner[1]}')
del users[prize_winner[1]]
def timer():
nowtime = datetime.now()
while True:
if nowtime.weekday() == 2 and nowtime.hour == 21 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}轮抽奖开始:')
users = aaa()
bbb(users)
ccc(users)
ddd(users)
time.sleep(1200)
today_date_str = datetime.now().strftime('%Y_%m_%d')
os.rename('updates.csv', '{}.csv'.format(today_date_str))
```
阅读全文