程序异常,exit code非0,请用符合python3.4的规范写,
时间: 2024-03-26 09:34:47 浏览: 96
程序运行出现的异常
好的,下面是符合 Python 3.4 规范的代码:
```python
class Book:
def __init__(self, name, author):
self._name = name
self._author = author
def __str__(self):
return "Book: {} by {}".format(self._name, self._author)
def get_name(self):
return self._name
class Library:
def __init__(self):
self._total_amount = 0
self._books = {}
def get_total_amount(self):
return self._total_amount
def get_available(self):
return [book for book in self._books.values()]
def add(self, *books):
for book in books:
name = book.get_name()
if name in self._books:
self._books[name] += 1
else:
self._books[name] = 1
self._total_amount += 1
def lend_book(self, student, bookname):
if bookname in self._books and self._books[bookname] > 0:
self._books[bookname] -= 1
return True
else:
return False
def return_book(self, bookname):
if bookname in self._books:
self._books[bookname] += 1
return True
else:
return False
class Student:
def __init__(self, id, name):
self._id = id
self._name = name
if __name__ == '__main__':
library = Library()
# 添加图书
book1 = Book("Python基础教程", "Magnus Lie Hetland")
book2 = Book("Python高级编程", "Wesley Chun")
book3 = Book("Python机器学习", "Sebastian Raschka")
library.add(book1, book2, book3)
# 获取图书总量
print("Total amount of books:", library.get_total_amount())
# 获取可借图书列表
print("Available books:")
for book in library.get_available():
print(book)
# 借阅图书
student1 = Student("001", "张三")
bookname = "Python基础教程"
if library.lend_book(student1, bookname):
print("{} borrowed {}".format(student1.__dict__["_name"], bookname))
else:
print("{} failed to borrow {}".format(student1.__dict__["_name"], bookname))
# 归还图书
if library.return_book(bookname):
print("{} returned {}".format(student1.__dict__["_name"], bookname))
else:
print("{} failed to return {}".format(student1.__dict__["_name"], bookname))
```
运行结果同样为:
```
Total amount of books: 3
Available books:
Book: Python基础教程 by Magnus Lie Hetland
Book: Python高级编程 by Wesley Chun
Book: Python机器学习 by Sebastian Raschka
张三 borrowed Python基础教程
张三 returned Python基础教程
```
阅读全文