-1
我创建了一个名为Greenfly的类。在这个类中,get_generations函数应该接受一个整数值。稍后在我的代码中,当我尝试在函数runmodel()
中使用self.generations
变量时,出现下面的错误。Python - TypeError:不支持的操作数类型为+:'type'和'int'
Traceback (most recent call last):
File "C:/Users/User/Documents/SCHOOL/C SCIENCE/Population model/Proper iteration.py", line 123, in <module>
juvenile.runmodel()
File "C:/Users/User/Documents/SCHOOL/C SCIENCE/Population model/Proper iteration.py", line 46, in runmodel
for n in range(1,g + 1):
TypeError: unsupported operand type(s) for +: 'type' and 'int'
class Greenfly:
def get_generations(self):
self.generations = int(input("Enter no. of generations:"))
while self.generations < 5 or self.generations > 25:
try:
self.generations = int(input("Please enter a whole number greater than 5 and less than 25.\n"))
if self.generations > 5 or self.generations < 25:
pass
else:
self.generations = int(input("Please enter a number greater than 5 and less than 25.\n"))
except ValueError:
print("This is not accepted.")
break
def runmodel(self):
# i == because the first generation is generation 0#
i = 0
g = self.generations
alteredpopulation = self.population * self.survival_rate
for n in range(1,g + 1):
i += 1
if i == self.generations:
print("Generation Zero Population:", self.population)
for n in range(i):
print(alteredpopulation)
alteredpopulation *= self.survival_rate
这是我所有的代码,如果你想看看它如何适应主程序。我不知道我是否正确使用类,但我不想使用全局变量,因为这不是'pythonic'。
import sys
class Greenfly:
population = float
survival_rate = float
birth_rate = float
generations = int
def getattributes(self):
self.population = float(input("Enter population numbers:"))
self.survival_rate = float(input("Enter survival rate:"))
def get_birthrate(self):
self.birth_rate = float(input("Enter birth rate:"))
def get_generations(self):
self.generations = int(input("Enter no. of generations:"))
while self.generations < 5 or self.generations > 25:
try:
self.generations = int(input("Please enter a whole number greater than 5 and less than 25.\n"))
if self.generations > 5 or self.generations < 25:
pass
else:
self.generations = int(input("Please enter a number greater than 5 and less than 25.\n"))
except ValueError:
print("This is not accepted.")
break
def displayattributes(self):
print("Population:", int(self.population))
print("Survival rate:", float(self.survival_rate))
def displayothers(self):
print("Birthrate:", float(self.birth_rate))
print("No. of generations:", int(self.generations))
def runmodel(self):
# i == because the first generation is generation 0#
i = 0
g = self.generations
alteredpopulation = self.population * self.survival_rate
for n in range(1,g + 1):
i += 1
if i == self.generations:
print("Generation Zero Population:", self.population)
for n in range(i):
print(alteredpopulation)
alteredpopulation *= self.survival_rate
class Mainframe(Greenfly):
def settings(self):
self.defaultSettings = int(input("\n[1] Set Generation 0 values."
"\n[2] Display Generation 0 values."
"\n[3] Disease factor."
"\n[4] Run model."
"\n[5] Export data."
"\n[6] Exit.\n"))
def resetmenu(self):
returnbutton = input("Press any key to return to the menu.")
Mainframe.settings(self)
def diseasefactor(self):
print("The disease factor works.")
Mainframe.resetmenu(self)
def exportdata(self):
print("Check excel for popualtion data.")
Mainframe.resetmenu(self)
print("AQA Population Model")
x = Mainframe()
x.settings()
juvenile = Greenfly()
adult = Greenfly()
senile = Greenfly()
while True:
try:
if x.defaultSettings == 1:
print("Set Generation Zero values.\n")
print("Juvenile:")
juvenile.getattributes()
print("\n")
print("Adult:")
adult.getattributes()
print("\n")
print("Senile:")
senile.getattributes()
print("\n")
print("Birth rate:")
x.get_birthrate()
print("No. of generations:")
x.get_generations()
x.resetmenu()
elif x.defaultSettings == 2:
print("Generation Zero values are as follows:")
print("Juvenile:")
juvenile.displayattributes()
print("Adult:")
adult.displayattributes()
print("Senile:")
senile.displayattributes()
x.displayothers()
x.resetmenu()
elif x.defaultSettings == 3:
x.diseasefactor()
x.resetmenu()
elif x.defaultSettings == 4:
juvenile.runmodel()
adult.runmodel()
senile.runmodel()
x.resetmenu()
elif x.defaultSettings == 5:
x.exportdata()
x.resetmenu()
elif x.defaultSettings == 6:
sys.exit("Thank you for using the AQA population model")
else:
print("Unable to compute your request.")
x.resetmenu()
except ValueError:
print("Not allowed.")
# This does not work if the first input is a float or string. Only works for integers. #
你不要在Python中声明变量; 'generations = int'将'int'类型赋值给'Greenfly.generations'。 – Ryan