python旅游景点管理系统代码
时间: 2024-06-24 10:00:25 浏览: 188
旅游信息管理代码
5星 · 资源好评率100%
Python 旅游景点管理系统可以是一个基于命令行或GUI的应用程序,用于存储、检索和管理景点信息。以下是一个简化的概念代码示例,使用了 Python 的基础数据结构和文件操作:
```python
class TouristSpot:
def __init__(self, id, name, location, description, rating):
self.id = id
self.name = name
self.location = location
self.description = description
self.rating = rating
class TouristSpotManager:
def __init__(self):
self.spots = []
# 添加景点
def add_spot(self, spot):
self.spots.append(spot)
# 查找景点
def search_spot(self, name):
for spot in self.spots:
if spot.name == name:
return spot
return None
# 保存到文件
def save_to_file(self, filename):
with open(filename, 'w') as file:
for spot in self.spots:
file.write(f"{spot.id}\t{spot.name}\t{spot.location}\t{spot.description}\t{spot.rating}\n")
# 从文件加载
def load_from_file(self, filename):
with open(filename, 'r') as file:
for line in file:
data = line.strip().split('\t')
self.add_spot(TouristSpot(int(data), data, data, data, float(data)))
# 示例用法
manager = TouristSpotManager()
manager.add_spot(TouristSpot(1, "故宫", "北京", "中国古代皇宫", 4.8))
manager.save_to_file('tourist_spots.txt')
# 加载景点
manager.load_from_file('tourist_spots.txt')
spot = manager.search_spot("故宫")
if spot:
print(f"找到景点 {spot.name},位于 {spot.location},评分:{spot.rating}")
```
阅读全文