1 回答

TA貢獻1772條經驗 獲得超5個贊
確保以Python 期望的確切格式輸入數字。
num1 = complex(input("Enter a number: "))
num2 = complex(input("Enter another number: "))
print(f"{num1} + {num2} = {num1 + num2}")
print(f"{num1} - {num2} = {num1 - num2}")
print(f"{num1} * {num2} = {num1 * num2}")
print(f"{num1} / {num2} = {num1 / num2}")
工作得很好(盡管有浮點精度):
$ python3 socom.py
Enter a number: 2.44-77.3333j
Enter another number: 6.83+15j
(2.44-77.3333j) + (6.83+15j) = (9.27-62.333299999999994j)
(2.44-77.3333j) - (6.83+15j) = (-4.390000000000001-92.3333j)
(2.44-77.3333j) * (6.83+15j) = (1176.6646999999998-491.5864389999999j)
(2.44-77.3333j) / (6.83+15j) = (-4.208867770125335-2.0791044579970688j)
添加回答
舉報