from song import *
from file import *
users = [{'name': 'gly', 'passwd': '123456'}]
def menu():
#顶层菜单函数
print('-'*30)
print('1.所有歌曲显示')
print('2.注册')
print('3.已有账户,登录')
print('0.退出')
print('-'*30)
def manger():
#管理者页面
print('-' * 30)
print('欢迎本系统管理者!!!')
print('-' * 30)
print('请选择您所需的功能:')
print('1. 删除一个用户')
print('2. 修改用户密码')
print('3. 查看一个用户信息')
print('4. 查看全部用户信息')
print('0. 退出管理者页面')
print('请选择您所需的功能(0-5):')
def user_menu():
#用户页面
print('-'*30)
print('欢迎来到歌曲小屋!')
print('-'*30)
print('1. 编辑歌曲')
print('2. 查询单首歌曲信息')
print('0. 退出您的歌曲小屋')
print('请选择您所需的功能(0-2):')
def menuedit():
#编辑功能菜单函数
print('-'*30)
print('1. 新增歌曲')
print('2. 删除歌曲')
print('3. 修改歌曲信息')
def printHead():
#打印歌曲信息表头
print('-'*15)
print('序号\t歌曲名\t歌手\t发行日期\t作词\t作曲\t流派')
with open("song.txt","r",encoding='utf-8')as f:
for line in f.readlines():
line=line.strip('\n')
print(line)
def sign_up():
#注册功能
global user
user_name = input("请输入您的用户名:")
user_passwd = input("请输入您的密码:")
user = {'name': user_name, 'passwd': user_passwd}
users.append(user)
print("注册成功!!!")
def login():
#登录功能
user_name = input("请输入您的用户名:")
user_passwd = input("请输入您的密码:")
count = 0
for i in users:
if user_name == i['name']:
count = 1
if user_passwd == i['passwd']:
print("登录成功!!!")
while_user(user_name)
else:
print("密码错误!!!")
print("请重试。")
login()
if count == 0:
print("用户名不存在!!!")
print("请重试。")
login()
def while_user(name):
#判断权限功能
while name: # 根据是否登陆成功,进入用户菜单
if name == 'gly': # 判断是否为管理者
manger() # 管理者页面
manger_n = input()
if manger_n == '1':
del_user() # 删除一个用户
elif manger_n == '2':
edit_user() # 修改用户密码
elif manger_n == '3':
look_user() # 查看一个用户信息
elif manger_n == '4':
look_users() # 查看全部用户信息
elif manger_n == '0':
break # 退出管理者页面
else:
print("非法输入!!!")
print("请再次选择:")
else:
user_menu() # 普通用户页面
user_n = input()
if user_n == '1':
editManage()
elif user_n == '2':
searchSong(songList,keyword,condition)
elif user_n == '0':
break # 退出普通用户页面
else:
print("非法输入!!!")
print("请再次选择:")
def del_user():
# 删除用户
user_name = input('请输入要删除的用户名:')
count = 0
index = 0
for i in users:
if user_name == i['name']:
count = 1
del users[index]
print('删除成功!!!')
index += 1
if count == 0:
print('查无此人!!!')
def edit_user():
# 修改用户
user_name = input('请输入要修改密码的用户名:')
count = 0
index = 0
for i in users:
if user_name == i['name']:
count = 1
del users[index]
new_user_name = user_name
new_user_passwd = input('请输入修改后的密码:')
new_user = {'name': new_user_name, 'passwd': new_user_passwd}
users.append(new_user)
print('修改成功!!!')
index += 1
if count == 0:
print('查无此人!!!')
def look_user():
# 查看一个用户
user_name = input('请输入要查看的用户名:')
count = 0
print('用户名\t密码\t')
for i in users:
if user_name == i['name']:
count = 1
print('%s\t\t%s\t' % (i['name'], i['passwd']))
if count == 0:
print('查无此人!!!')
def look_users():
# 查看全部用户
print('用户名\t密码\t')
for i in users:
print('%s\t\t%s\t' % (i['name'], i['passwd']))
def searchSong(songList,keyword,condition):
#单首歌曲信息查询功能
result = []
for i in range(len(songList)):
if condition == "序号" and songList[i].getNum(songList)==keyword:
result.append(i)
elif condition == "歌曲名称" and songList[i].getName()==keyword:
result.append(i)
elif condition == "歌手" and songList[i].getSinger()==keyword:
result.append(i)
elif condition == "发行年月" and songList[i].getdate()==keyword:
result.append(i)
elif condition == "作词" and songList[i].getlyrics()==keyword:
result.append(i)
elif condition == "作曲" and songList[i].getcomposer()==keyword:
result.append(i)
elif condition == "流派" and songList[i].getstyle()==keyword:
result.append(i)
return result
def editManage():
#数据编辑功能
while True: #按序号进行插入删除修改,序号不能重复
menuedit()
#显示对应的二级菜单
global songList
choice=input("请输入您的选择(0-3):")
if choice=='1': #读入一首新歌曲
readsong(songList,n=20)
elif choice=='2': #删除原歌曲
num=input("请输入需要删除的歌曲序号:")
deletesong(songList,num) #调用函数删除指定序号的歌曲信息
elif choice=='3': #修改原歌曲
num=input("请输入需要修改的歌曲序号:")
found=searchSong(songList,num,"序号")
#调用函数查找指定序号的歌曲信息
if found!=[]: #如果该序号的信息存在
newSong=[]
readsong(newSong,1) #读入一条完整的歌曲信息
else:
print("该歌曲不存在,无法修改其信息!")
else:
break
def deletesong(songList,num):
#删除功能
for song in songList: #寻找待删除的元素
if song.getNum()==num: #如果找到相等元素
songList.remove(song) #删除对应的元素
print("已删除指定序号的歌曲信息")
break
else: #如果找不到待删除的元素
print("该歌曲不存在,删除失败!") #给出提示信息后返回
def runMain(choice):
#主控模板,对应于下一级菜单其下各功能选择执行
if choice=='1':
printHead() #1.打印歌曲信息表格
elif choice=='2':
sign_up() #2.注册
elif choice=='3':
login() #3.已有账户,登录
if __name__=="__main__":
rfile = open("song.txt", 'r',encoding='UTF-8')
songList = rfile.readlines()
rfile.close()
for line in songList:
line = line.replace('\n', '')
print(line)
print('欢迎您使用歌曲信息系统')
while True:
menu() #显示主菜单
choice = input("请输入您的选择(0-3):")
if choice=='0':
print("感谢您的使用,再见!")
break #退出循环,停止接收用户的输入
elif choice>'0' and choice<='3':
runMain(choice) #通过调用此函数进行一级功能项的选择执行
else:
print("输入错误,请重新输入!")
这是主程序
from song import *
#son为存储song类的对象的列表
def createFile(songList): #建立初始的数据文件
try:
with open('songs.txt','w',encoding="UTF-8") as file:
print("请初始化歌曲数据:")
print("-"*30)
readSon(songList) #调用song.py中的函数读入数据
tab=','
for oneSon in songList: #将刚才读入的所有记录一次性写入文件
s=str(oneSong.getNum()+tab+oneSong.getName()+tab+oneSong.getSinger()+tab+oneSong.getLyricist()+tab+oneSong.getComposer()+tab+oneSong.getDate()+tab+oneSong.getStyle())
file.write(s)
return len(songList)
except IOError:
print("文件打开错误!") #若打开失败,输入提示信息
exit() #然后退出
def readFile(songList): #将文件的内容读出置于对象列表son中
try:
with open('songs.txt','r',encoding="UTF-8") as file:
for line in file.readlines():
if line!="":
s=line.rstrip('\n').split(',')
num,name,singer,date,lyricist,composer,style = s[0],s[1],s[2],s[3],s[4],s[5],s[6]
oneSong = Song(num,name,singer,lyricist,composer,date,style)
songList.append(oneSong)
return(songList) #返回记录条数
except FileNotFoundError:
print("数据文件不存在!") #若打开失败,输入提示信息
return 0 #因为数据文件不存在,返回0,表示无歌曲数据
def saveFile(songList): #将对象列表的内容写入文件
try:
with open('songs.txt','w',encoding="UTF-8") as file:
tab='\t'
for oneSong in songList:
s=str(oneSong.getNum()+tab+oneSong.getName()+tab+oneSong.getSinger()+tab+oneSong.getLyricist()+tab+oneSong.getComposer()+tab+oneSong.getDate()+tab+oneSong.getStyle())
file.write(s)
except IOError:
print("文件打开错误!") #如果打开失败,输出信息
exit(0) #然后退出
这是file.py文件
class song(object):
#歌曲信息类
def __init__(self, num, name, singer, date, lyrics, composer,style):
self.__num = num #歌曲序号
self.__name = name #歌曲名
self.__singer = singer #歌手名
self.__date = date #发行时间
self.__lyrics = lyrics #作词
self.__composer = composer #作曲
self.__style = style #流派
def getInfo(self):
info = {}
info["num"] = self.__num
info["name"] = self.__name
info["singer"] = self.__singer
info["date"] = self.__date
info["lyrics"] = self.__lyrics
info["composer"] = self.__composer
info["style"] = self.__style
return info
def getNum(self):
return self.__num
def getName(self):
return self.__name
def getsinger(self):
return self.__singer
def getdate(self):
return self.__date
def getlyrics(self):
return self.__lyrics
def getcomposer(self):
return self.__composer
def getstyle(self):
return self.__style
def readsong(songList,n=20):
#读入歌曲记录值,序号为0或读满规定条数记录时停止
global lyrics
global style
while n>0:
print("请输入一首歌曲的详细信息(序号为0时结束输入):")
num = input("序号:") #输入序号
if num == '0': #序号为0停止输入
break
else:
if find(songList,num,"序号")==[]:
#序号相同不允许插入,保证序号的唯一性
print("列表中存在相同的序号,禁止插入!")
return len(songList)
name = input("歌曲名:") #输入歌曲名
singer = input("歌手名:") #输入歌手名
print("请输入与该歌曲相关的时间")
date= input("发行时间:")
lyrics = input("作曲:")
composer = input("作词:")
style = input("流派:")
onesong = song(num, name, singer, date, lyrics, composer, style)
songList.append(onesong)
print("-"*30)
n=n-1
return len(songList) #返回实际读入的记录条数
def printsong(songList):
#输出所有歌曲记录的值
for song in songList:
print(song.getNum(),song.getName(),song.getsinger(),sep='\t',end='\t')
print(song.getdate())
print(song.getlyrics())
print(song.getcomposer())
print(song.getstyle())
def find(songList,keyword,condition): #集合 关键字 查找条件
result = []
for song in songList:
if condition == "序号" and song.getNum()==keyword:
result.append(song)
elif condition == "歌曲名" and song.getName()==keyword:
result.append(song)
elif condition == "歌手" and song.getSinger()==keyword:
result.append(song)
elif condition == "作词" and song.getLyricist()==keyword:
result.append(son)
elif condition == "作曲" and song.getComposer()==keyword:
result.append(song)
elif condition == "发行日期" and song.getDate()==keyword:
result.append(song)
elif condition == "流派" and song.getStyle()==keyword:
result.append(song)
return result
这是song.py文件
主程序运行到编辑歌曲功能报错
Traceback (most recent call last):
File "C:\Users\lenovo\Desktop\song\5.0.py", line 247, in <module>
runMain(choice) #通过调用此函数进行一级功能项的选择执行
File "C:\Users\lenovo\Desktop\song\5.0.py", line 229, in runMain
login() #3.已有账户,登录
File "C:\Users\lenovo\Desktop\song\5.0.py", line 72, in login
while_user(user_name)
File "C:\Users\lenovo\Desktop\song\5.0.py", line 107, in while_user
editManage()
File "C:\Users\lenovo\Desktop\song\5.0.py", line 195, in editManage
readsong(songList,n=20)
File "C:\Users\lenovo\Desktop\song\song.py", line 54, in readsong
if find(songList,num,"序号")==[]:
File "C:\Users\lenovo\Desktop\song\song.py", line 83, in find
if condition == "序号" and song.getNum()==keyword:
AttributeError: 'str' object has no attribute 'getNum'
运行查询功能报错
Traceback (most recent call last):
File "C:\Users\lenovo\Desktop\song\5.0.py", line 247, in <module>
runMain(choice) #通过调用此函数进行一级功能项的选择执行
File "C:\Users\lenovo\Desktop\song\5.0.py", line 229, in runMain
login() #3.已有账户,登录
File "C:\Users\lenovo\Desktop\song\5.0.py", line 72, in login
while_user(user_name)
File "C:\Users\lenovo\Desktop\song\5.0.py", line 109, in while_user
searchSong(songList,keyword,condition)
NameError: name 'keyword' is not defined
新人求问该怎么修改?
不知道你这个问题是否已经解决, 如果还没有解决的话: