Ai
1 Star 0 Fork 0

cf2006/Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
5.py 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
cf2006 提交于 2025-04-16 20:33 +08:00 . 提交说明
#类和对象 太空星球探索
import random
class Planet:
def __init__(self,name,resources):
self.name = name
self.resources = resources
self.explored = False
def explore(self):
if not self.explored:
found_resources = random.randint(1,self.resources)
print(f'在{self.name}星球上找到了{found_resources}单位的资源。')
self.explored = True
else:
print(f'{self.name}星球已经被探索过了。')
class Spaceship:
def __init__(self,name):
self.name = name
self.discovered_resources = 0
def visit_planet(self,planet):
planet.explore()
if planet.explored:
self.discovered_resources += planet.resources
print(f'飞船{self.name}{planet.name}星球上发现了{planet.resources}单位的资源。')
else:
print(f'飞船{self.name}{planet.name}星球上没有发现资源。')
#创建星球对象
planet1 = Planet('火星',100)
planet2 = Planet('土星',200)
# 创建飞船对象
spaceship = Spaceship('探索者')
spaceship.visit_planet(planet1)
spaceship.visit_planet(planet2)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cf2006/python.git
git@gitee.com:cf2006/python.git
cf2006
python
Python
master

搜索帮助