编写个程序,求出两个整数的和,差,积,商(完整的)还有整商,余数
时间: 2024-10-17 14:08:08 浏览: 19
编写一个简单的Python程序,可以计算两个整数的和、差、积、商以及整除后的商和余数:
```python
def calculate(a, b):
# 计算和、差
sum_result = a + b
difference = a - b
# 计算积、商
product = a * b
quotient = a / b
# 商和余数(处理浮点数除法)
try:
int_quotient = a // b
remainder = a % b
except ZeroDivisionError:
int_quotient = None
remainder = None
print("注意:除数不能为0")
return {
"和": sum_result,
"差": difference,
"积": product,
"商": float(quotient), # 返回浮点数表示精确的小数商
"整商": int_quotient, # 整除商,如果除数为0则为None
"余数": remainder,
}
# 示例输入
num1 = 15
num2 = 4
result = calculate(num1, num2)
print(f"对于数字 {num1} 和 {num2}:")
for key, value in result.items():
print(f"{key}: {value}")
阅读全文