我做了一个练习来学习Python,它不是';行不通
这段代码没有运行,我不知道为什么。我花了无数个小时寻找,但什么也找不到。我让朋友们检查了一下,他们似乎都没有发现问题。你能帮我找出哪里不对劲吗?问题似乎与子类中的subspecies变量有关,但我不确定如何修复它。先谢谢你我做了一个练习来学习Python,它不是';行不通,python,python-3.x,Python,Python 3.x,这段代码没有运行,我不知道为什么。我花了无数个小时寻找,但什么也找不到。我让朋友们检查了一下,他们似乎都没有发现问题。你能帮我找出哪里不对劲吗?问题似乎与子类中的subspecies变量有关,但我不确定如何修复它。先谢谢你 class Species: """A simple attempt to represent a species""" def __init__(self, genus, species, author, year): """Initial
class Species:
"""A simple attempt to represent a species"""
def __init__(self, genus, species, author, year):
"""Initialize attributes to describe a species"""
self.genus = genus
self.species = species
self.author = author
self.year = year
self.avg_male_body_mass = 0
def get_descriptive_binomen(self):
"""Return a neatly formatted binomial name"""
full_binomen = f"{self.genus.title()} {self.species}"
full_binomen += f"\n{self.author.title()}, {self.year} \n"
return full_binomen
def get_avg_male_body_mass(self):
"""Print a statement showing the average body of mass of species males"""
print(
f"The average male {full_binomen} has a body mass of {self.avg_male_body_mass} kilograms.")
def update_body_mass(self, mass):
"""Set the average male body mass to a given value
Reject the change if it attempts to make the mass smaller"""
if mass >= self.avg_male_body_mass:
self.avg_male_body_mass = mass
else:
print("Elephants cant't shrink!")
def increment_body_mass(self, kilograms):
"""Add the given amount to the average male body mass"""
self.avg_male_body_mass += kilograms
class Subspecies(Species):
"""Represent aspects of a species, specific to subspecies"""
#The parentheses allow the child class, Subspecies, to inherit the
#characteristics of the parent class, Species
def __int__(self, genus, species, author, year):
"""
Initialize aributes of the parent class.
Then initialize attributes specific to a subspecies.
"""
super().__init__(genus, species, author, year)
self.subspecies = 'sumatranus'
#Superclass is another name for parent class, and subclass
#is another name for child class
def get_descriptive_trinomen(self):
"""Print statement with full trinomial name."""
print(f"{self.genus.title()} {self.species} {self.subspecies} \n {self.author.title()} {self.year}")
sumatran_elephant = Subspecies('elephas','maximus', 'temminck', '1847')
print(sumatran_elephant.get_descriptive_binomen())
sumatran_elephant.get_descriptive_trinomen()
看起来您在
def\uu int\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuui(自我、属、种、作者、年份):
您确切地说“它不运行”和“它不工作”是什么意思?欢迎来到StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。“它不工作”不是问题。@juanpa.arrivillaga抱歉,这是我第一次发布。我的意思是它不会在没有错误的情况下执行。我只是在学习Python,所以对于我所犯的任何错误,我深表歉意。@TrentGrasso这很好,但请看一看并了解如何构造一个广受欢迎的问题。仅仅抛弃你的全部代码并说“它不工作”是不会被接受的。帮助我们,帮助你。