Day3では先日作った計算機の改善版を作成しました。
今回学んだこと
・ except を複数使う方法について
try-except文において、exceptを複数使い、エラーの種類に応じて処理を分岐させることができる。
・raise について
raiseは、意図的にエラーを発生させる。ValueErrorやZeroDivisionErrorのほかに、Exceptionというのがあり、このExceptionはすべてのエラーをキャッチする。
すべてのエラーをキャッチするので、基本的に最後に置く。
・continue について
ループの最初に戻るために使う。
#Day3 Project -- Simple Calculator Modified
while True:
try:
num1 = float(input("Enter a number: "))
operator = input("Choose an operator: +, -, *, /, **, %: ").strip()
num2 = float(input(f"Enter a number you want to {operator} to {num1}: "))
except ValueError:
print("Invalid input. Please enter numbers.")
continue
if operator not in ("+", "-", "*", "/", "**", "%"):
print("Invalid operator. Please enter one of the following: +, -, *, /, **, %")
continue
try:
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 == 0:
raise ZeroDivisionError("You can't divide by zero.")
result = num1 / num2
elif operator == "**":
result = num1 ** num2
elif operator == "%":
result = num1 % num2
print(f"{num1} {operator} {num2} = {result}")
except ZeroDivisionError as e:
print(e)
except OverflowError:
print("The calculated result is too large to handle!")
continue
if user_choice != "c":
print("End of calculation.")
break