-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