威脅級別計算正確,但其余屬性要么未顯示,要么順序錯誤。在此處輸入圖片說明
如果你能幫助我,這是代碼:
class animal():
def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0):
self.animal_species = animal_species
self.age = age
self.hunger_level = hunger_level
self.threat_level = threat_level
def create():
animal_species = input("Enter species: ")
age = int(input("Enter age: "))
return animal(animal_species, age)
def changeThreat_level():
hunger_level = int(input("Enter hunger(1-10): "))
threat_level = None
if hunger_level <= 3:
threat_level = 'peaceful'
elif hunger_level >=4 and hunger_level<=7:
threat_level = 'narky'
else:
threat_level = 'aggressive'
return animal(hunger_level, threat_level)
cat = create()
cat = changeThreat_level()
print("Animal's Attributes: ")
print("-------------------------")
print("The species of the animal is", cat.animal_species)
print("The age of the animal is", cat.age)
print("The animals threat level is", cat.threat_level)
print("The animals hunger out of 10 is around", cat.hunger_level)
uj5u.com熱心網友回復:
你的回報changeThreat_level是把hunger_levelandthreat_level值放在建構式期望的地方animal_speciesand age。您可以嘗試指定欄位:
return animal(hunger_level=hunger_level, threat_level=threat_level)
最好還是傳入動物的實體并更改它的屬性。這樣的事情應該作業:
class animal():
def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0):
self.animal_species = animal_species
self.age = age
self.hunger_level = hunger_level
self.threat_level = threat_level
def create():
animal_species = input("Enter species: ")
age = int(input("Enter age: "))
return animal(animal_species, age)
def changeThreat_level(animal:animal):
hunger_level = int(input("Enter hunger(1-10): "))
threat_level = None
if hunger_level <= 3:
threat_level = 'peaceful'
elif hunger_level >=4 and hunger_level<=7:
threat_level = 'narky'
else:
threat_level = 'aggressive'
animal.hunger_level = hunger_level
animal.threat_level = threat_level
cat = create()
changeThreat_level(cat)
print("Animal's Attributes: ")
print("-------------------------")
print("The species of the animal is", cat.animal_species)
print("The age of the animal is", cat.age)
print("The animals threat level is", cat.threat_level)
print("The animals hunger out of 10 is around", cat.hunger_level)
uj5u.com熱心網友回復:
OOP 原則之一是封裝,當每個物件在類中保持其狀態為私有時就可以實作。考慮使用 getter 和 setter 進行物件操作。這樣你就不會遇到這種問題。
此外,您的標識是錯誤的,因為create和changeThreat_level是 Cat 物件的方法,它們必須嵌套在類下方。此外,由于cat是animal物件的一個實體,請考慮將 cat 初始化為以下內容:
class animal():
def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0):
self.animal_species = animal_species
self.age = age
self.hunger_level = hunger_level
self.threat_level = threat_level
def create(self):
self.animal_species = input("Enter species: ")
self.age = int(input("Enter age: "))
self.hunger_level = int(input("Enter hunger(1-10): "))
def change_threat_level(self):
if self.hunger_level <= 3:
self.threat_level = 'peaceful'
elif self.hunger_level >=4 and self.hunger_level<=7:
self.threat_level = 'narky'
else:
self.threat_level = 'aggressive'
def print_animal_information(self):
print("Animal's Attributes: ")
print("-------------------------")
print("The species of the animal is", self.animal_species)
print("The age of the animal is", self.age)
print("The animals threat level is", self.threat_level)
print("The animals hunger out of 10 is around", self.hunger_level)
cat = animal()
cat.create()
cat.change_threat_level()
cat.print_animal_information()
現在你的代碼作業正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337970.html
上一篇:搜索專案是否在物件串列中
