如何定义一个包含图书信息(书号、书名、价格)的顺序表 python
时间: 2024-06-12 08:09:05 浏览: 140
要定义一个包含图书信息(书号、书名、价格)的顺序表,可以使用Python的列表来实现。每个元素代表一本图书,包含书号、书名和价格三个属性。以下是一个示例代码:
```python
# 定义一个包含图书信息的顺序表
book_list = []
# 添加图书信息
def add_book(book_list, book_no, book_name, price):
book = [book_no, book_name, price]
book_list.append(book)
# 创建图书信息表
add_book(book_list, 1, "Book A", 10.99)
add_book(book_list, 2, "Book B", 15.99)
add_book(book_list, 3, "Book C", 12.99)
# 计算所有图书的平均价格
def calculate_average_price(book_list):
total_price = 0
for book in book_list:
total_price += book[2]
average_price = total_price / len(book_list)
return average_price
average_price = calculate_average_price(book_list)
# 提高低于平均价格的图书价格20%
def increase_price(book_list, average_price):
for book in book_list:
if book[2] < average_price:
book[2] *= 1.2
increase_price(book_list, average_price)
# 打印图书信息表
for book in book_list:
print("书号:", book[0], "书名:", book[1], "价格:", book[2])
```
阅读全文