2018-09-03课后作业(day11)

1.提取data.json中的数据,将每条数据中的name、text、love和comment信息。并且保存到另外一个json文件中

import json
with open('./data.json','r',encoding='utf-8') as f:
    work = json.load(f)
    data1 = []
    data2 = []
    data3 = []
    data4 = []
    data = {'name':data1,'text':data2,'love':data3,'comment':data4}
    for x in range (len(work['data'])):
        data1.append(work['data'][x]['name'])
        data2.append(work['data'][x]['text'])
        data3.append(work['data'][x]['love'])
        data4.append(work['data'][x]['comment'])
with open('./new_data','w',encoding='utf-8') as ff:
    json.dump(data,ff)

2.统计data.json中comment数量超过1000的个数并且将其值全部全程

import json
with open('./data.json','r',encoding='utf-8') as f:
    work = json.load(f)
    data1 = []
    for x in range(len(work['data'])):
        data1.append(work['data'][x]['comment'])
    count = 0
    for i in data1:
        if int(i) > 1000:
            count += 1
    print(count)

3.将data.json文件中所有点赞数(love)对应的值超出1000的用k来表示,例如1000修改为1k, 1345修改为1.3k

import json
with open('./data.json','r',encoding='utf-8') as f:
    work = json.load(f)
    for x in range (len(work['data'])):
        if int(work['data'][x]['love']) > 1000:
            number = int(work['data'][x]['love'])/1000
            work['data'][x]['love'] = '%.1fk'%number
with open('./new_data1.json','w',encoding='utf-8') as ff:
    json.dump(work,ff)

4.写猜数字游戏,如果输入有误,提示重新输入,直达输入正确为止。比如:输入数字的时候没有按要求输入,提示重新输入

import random
number = random.randint(0,100)
while True:
    try:
        num = int(input('请输入0-100的整数:'))
    except ValueError:
        print('请按照要求来输入数字!')
        continue
    if int(num) == number:
        print('猜对了!')
        break
    elif int(num) > number:
            print('大了!')
    else:
        print('小了!')

5.写学生管理系统的添加学生功能(数据需要本地化),要求除了保存学生的基本信息以外还要保存学生的学号,但是学号需要自动生成

你可能感兴趣的:(2018-09-03课后作业(day11))