类中变量在未赋值前引用,可是想不明白
import keyboardimport time
bookinformation = []
bookinformation_number = 0
class Menu:
def showmenu(self):
print('''欢迎来到图书管理系统
添加图书 —— 1
查询图书 —— 2
借阅图书 —— 3
归还图书 —— 4
删除图书 —— 5''')
if keyboard.is_pressed('1'):
Bookmanager.addbooks()
class Bookmanager:
def addbooks(self):
while True:
information = input('请依次输入书的名字、作者、类型、添加日期,以及备注(请用空格分隔不同的信息)\n')
information += ' '
information_mediator = []
one_information = ''
information_array = []
whether_to_execute = 0
for i in information:
if i == ' ':
whether_to_execute += 1
if whether_to_execute >= 5:
for character in information:
if character.encode() == ' '.encode():
for character in information_mediator:
one_information += character
if one_information != '':
information_array.append(one_information)
one_information = ''
information_mediator = []
else:
information_mediator.append(character)
name = information_array
author = information_array
genre = information_array
add_date = information_array
marker = information_array
del information_array
bookinformation.append(information_array)
bookinformation_number += 1
print('''成功添加图书
新书名字是 %s
作者是 %s
类型是 %s
添加日期是 %s
备注是 %s'''%(name,author,genre,add_date,marker))
print('如添加有误,请按F键;正确请按J键')
while True:
try:
if keyboard.is_pressed('f'):
del bookinformation
print('已删除此信息,开始重新添加')
bookinformation_number -= 1
break
elif keyboard.is_pressed('j'):
break
except:
print('')
time.sleep(0.2)
print('继续添加图书信息请按F键,退出请按J键')
while True:
try:
if keyboard.is_pressed('f'):
break
elif keyboard.is_pressed('j'):
return
except:
print('')
else:
print('您的输入不完全或者有误,请重新输入')
buffer = input('请忽略本行内容,继续敲击回车键')
test = Bookmanager()
test.addbooks()
print(bookinformation)
还有我迫不得已才加了个buffer,有大神有什么好的办法没? 修改全局变量,声明关键字了吗? Stubborn 发表于 2021-5-7 21:20
修改全局变量,声明关键字了吗?
import keyboard
import time
bookinformation = []
bookinformation_number = 0
class Menu:
def showmenu(self):
print('''欢迎来到图书管理系统
添加图书 —— 1
查询图书 —— 2
借阅图书 —— 3
归还图书 —— 4
删除图书 —— 5''')
if keyboard.is_pressed('1'):
Bookmanager.addbooks()
class Bookmanager:
def addbooks(self):
global bookinformation_number
global bookinformation
while True:
information = input('请依次输入书的名字、作者、类型、添加日期,以及备注(请用空格分隔不同的信息)\n')
information += ' '
information_mediator = []
one_information = ''
information_array = []
whether_to_execute = 0
for i in information:
if i == ' ':
whether_to_execute += 1
if whether_to_execute >= 5:
for character in information:
if character.encode() == ' '.encode():
for character in information_mediator:
one_information += character
if one_information != '':
information_array.append(one_information)
one_information = ''
information_mediator = []
else:
information_mediator.append(character)
name = information_array
author = information_array
genre = information_array
add_date = information_array
marker = information_array
del information_array
bookinformation.append(information_array)
bookinformation_number += 1
print('''成功添加图书
新书名字是 %s
作者是 %s
类型是 %s
添加日期是 %s
备注是 %s'''%(name,author,genre,add_date,marker))
print('如添加有误,请按F键;正确请按J键')
while True:
try:
if keyboard.is_pressed('f'):
del bookinformation
print('已删除此信息,开始重新添加')
bookinformation_number -= 1
break
elif keyboard.is_pressed('j'):
break
except:
print('')
time.sleep(0.2)
print('继续添加图书信息请按F键,退出请按J键')
while True:
try:
if keyboard.is_pressed('f'):
break
elif keyboard.is_pressed('j'):
return
except:
print('')
else:
print('您的输入不完全或者有误,请重新输入')
buffer = input('请忽略本行内容,继续敲击回车键')
test = Bookmanager()
test.addbooks()
print(bookinformation)
Stubborn 发表于 2021-5-7 21:20
修改全局变量,声明关键字了吗?
谢谢,我查了下资料,解决了 YouCam 发表于 2021-5-7 21:45
谢谢,我查了下资料,解决了
OK
页:
[1]