网站首页 > 技术文章 正文
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
raise ValueError("除数不能为0")
return x / y
def calculator():
print("请选择运算:")
print("1. 加法")
print("2. 减法")
print("3. 乘法")
print("4. 除法")
choice = input("请输入运算序号(1/2/3/4): ")
num1 = float(input("请输入第一个数字: "))
num2 = float(input("请输入第二个数字: "))
if choice == '1':
print("结果:", add(num1, num2))
elif choice == '2':
print("结果:", subtract(num1, num2))
elif choice == '3':
print("结果:", multiply(num1, num2))
elif choice == '4':
try:
print("结果:", divide(num1, num2))
except ValueError as e:
print(e)
else:
print("无效的输入,请输入正确的运算序号")
if __name__ == "__main__":
calculator()
猜你喜欢
- 2024-12-15 一文了解 “*” 星号在 Python 中的多种用法
- 2024-12-15 什么是序列,Python序列详解(包括序列类型和常用操作)
- 2024-12-15 python中pyd文件的使用 py文件edit with idle
- 2024-12-15 如何用python代码实现手动输入两个数,并实现两个数的运算
- 2024-12-15 python一行代码打印「迷宫」建议收藏
- 2024-12-15 Python和C语言中的几个比较容易混淆的运算符号
- 2024-12-15 python 加、减、乘、除、乘方运算符
- 2024-12-15 python每天学习一点点(列表中+、*号、extend()方法的使用方法)
- 2024-12-15 简单学Python——内置函数22——range()函数
- 2024-12-15 用Python的numpy库进行线性拟合或者多项式拟合
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)