首页新闻找找看学习计划

python列表中元素间插入空行

0
悬赏园豆:10 [已解决问题] 解决于 2017-07-17 18:30

我写了一个手动输入后收集专辑信息的程序,写的很繁杂......是定义了一个可以将实参整理入字典的函数,再将字典放入列表
album_list = []
def album(name, singer, quantities_of_songs):
"""record the info of a album"""
info = {'name': a, 'singer': b, 'numbers ': c}
return info
while True:
a = input("input the name.\ninput 'q' to exit\n")
if a == 'q' :
b = c = 'q'
break
b = input("input the singer's name\n")
if b == 'q':
c = 'q'
break
c = input("input the number of songs\n")
if c == 'q':
break
recorded_album = album(a, b, c)
album_list.append(recorded_album)
question = input("do you have any other album?(yes/no)")
if question == 'no':
break
if a and b and c != 'q':
print("\n"*3)
print(album_list)

然而最后print出来的list是多个字典紧挨着排列在一起的,能否在每个元素之间插入空行使结构整洁一点

skrrr的主页 skrrr | 初学一级 | 园豆:110
提问于:2017-07-17 17:42
< >
分享
最佳答案
0

把代码排版一下,再发,Python 排版不一样结果天壤之别

print不能直接打印dict类型的数据,先转换成json格式,

print('\n'.join(album_list)) 这个可以打印换行

 

import json

album_list = []

def album(name, singer, quantities_of_songs):
    """record the info of a album"""
    info = {'name': name, 'singer': singer, 'numbers ': quantities_of_songs}
    return info

while True:
    a = input("input the name.\ninput 'q' to exit\n")
    if a == 'q':
        b = c = 'q'
        break
    b = input("input the singer's name\n")
    if b == 'q':
        c = 'q'
        break
    c = input("input the number of songs\n")
    if c == 'q':
        break
    recorded_album = album(a, b, c)
    info = json.dumps(recorded_album)
    album_list.append(info)
    question = input("do you have any other album?(yes/no)")
    if question == 'no':
        break
    if a and b and c != 'q':
        print("\n" * 3)
        print('\n'.join(album_list))
  

 

收获园豆:10
再见2019 | 专家六级 |园豆:11577 | 2017-07-17 17:59

啊还没学过什么import json

到时候我再回来看

谢谢

 

skrrr | 园豆:110 (初学一级) | 2017-07-17 18:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册