前提・実現したいこと
pythonで数学の計算問題を作りたい
発生している問題・エラーメッセージ
問題・解答を表示するprint_eqとprint_ansを実装中に以下のエラーメッセージが発生しました。
実行すれば必ずエラーが発生するというわけではなく、self.varとself.ans1とself.ans2の型は確認したのですが...
問27:<class 'str'> {}^2 -{} Traceback (most recent call last): File "calc.py", line 116, in <module> tenkai[i-1].print_ans() File "C:\---\---\calc\Quadratic.py", line 83, in print_ans print("{}^2 -{}").format(self.var, self.var) AttributeError: 'NoneType' object has no attribute 'format'
該当のソースコード
python3
1#-------------解の最大最小の決定 2max = 15 3min = -15 4vars = ["a", "b", "c", "t", "m", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"] 5 6 7#-------------------以下乗法公式関係---------------------------------------- 8 9class tenkai: 10 def __init__(self): 11 self.var = random.choice(vars) 12 b = random.randint(1, 7) 13 14 if b == 1: 15 self.ans1 = random.randint(min, max) 16 self.ans2 = self.ans1 17 18 elif b == 2: 19 self.ans1 = random.randint(min, max) 20 self.ans2 = -self.ans1 21 22 else: 23 self.ans1 = random.randint(min, max) 24 self.ans2 = random.randint(min, max) 25 26 if self.ans1 == 0 and self.ans2 == 0: 27 a = random.randint(1, 3) 28 if a == 1: 29 self.ans1 += random.randint(1, max+1) 30 self.ans2 = self.ans1 31 elif a == 2: 32 self.ans1 += -random.randint(1, max+1) 33 self.ans2 = self.ans1 34 else: 35 self.ans1 += random.randint(1, max+1) 36 self.ans2 = -self.ans1 37 else: 38 pass 39 40 def adabs(self, a): 41 if a > 0: 42 return "+{}".format(str(abs(a))) 43 elif a == 0: 44 return "+0" 45 elif a < 0: 46 return "-{}".format(str(abs(a))) 47 else: 48 pass 49 50 def print_eq(self): 51 print(type(self.var)) 52 if self.ans1 == self.ans2: 53 print("({} {})^2".format(self.var, self.adabs(self.ans1))) 54 elif self.ans1 == 0: 55 print("{}({} {})".format(self.var, self.var, self.adabs(self.ans2))) 56 elif self.ans2 == 0: 57 print("{}({} {})".format(self.var, self.var, self.adabs(self.ans1))) 58 else: 59 print("({} {})({} {})".format(self.var, self.adabs(self.ans1), self.var, self.adabs(self.ans2))) 60 61 def print_ans(self): 62 print(type(self.var)) 63 if self.ans1 + self.ans2 == 1 and self.ans1*self.ans2 == 0: 64 print("{}^2 +{}").format(self.var, self.var) 65 66 elif self.ans1 + self.ans2 == 1 and self.ans1*self.ans2 != 0: 67 print("{}^2 +{} {}".format(self.var, self.var, self.adabs(self.ans1*self.ans2))) 68 69 elif self.ans1 + self.ans2 == -1 and self.ans1*self.ans2 == 0: 70 print("{}^2 -{}").format(self.var, self.var) 71 72 elif self.ans1 + self.ans2 == -1 and self.ans1*self.ans2 != 0: 73 print("{}^2 -{} {}".format(self.var, self.var, self.adabs(self.ans1*self.ans2))) 74 75 elif self.ans1 + self.ans2 != 1 and self.ans1 + self.ans2 != -1 and self.ans1*self.ans2 == 0: 76 print("{}^2 {}{}".format(self.var, self.adabs(self.ans1+self.ans2), self.var)) 77 78 elif self.ans1 + self.ans2 == 0 : 79 print("{}^2 {}".format(self.var, self.adabs(self.ans1*self.ans2))) 80 81 else: 82 print("{}^2 {}{} {}".format(self.var, self.adabs(self.ans1+self.ans2), self.var, self.adabs(self.ans1*self.ans2))) 83
試したこと
問題となっているであろうself.varとself.ans1とself.ans2の型は確認しました
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/19 16:59