网站首页 > 技术文章 正文
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库进行线性拟合或者多项式拟合
- 07-06Python学不会来打我(19)循环语句while/for的使用方法与实战案例
- 07-06python入门-day5-循环语句(python循环语句总结)
- 07-06Python循环:重复的力量(python中如何重复循环程序)
- 07-06编程小白学做题:Python 的经典编程题及详解,附代码和注释(一)
- 07-06python 简述列表推导式和生成器(python列表举例)
- 07-06Python列表推导式:让你的代码优雅如诗!
- 07-06Python中while循环详解(python中while循环的执行过程)
- 07-06Python自学|while循环的使用方法|99乘法口诀表倒着打印
- 274℃Python短文,Python中的嵌套条件语句(六)
- 273℃python笔记:for循环嵌套。end=""的作用,图形打印
- 271℃PythonNet:实现Python与.Net代码相互调用!
- 265℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 265℃Python实现字符串小写转大写并写入文件
- 125℃原来2025是完美的平方年,一起探索六种平方的算吧
- 106℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 104℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python学不会来打我(19)循环语句while/for的使用方法与实战案例
- python入门-day5-循环语句(python循环语句总结)
- Python循环:重复的力量(python中如何重复循环程序)
- 编程小白学做题:Python 的经典编程题及详解,附代码和注释(一)
- python 简述列表推导式和生成器(python列表举例)
- Python列表推导式:让你的代码优雅如诗!
- Python中while循环详解(python中while循环的执行过程)
- Python自学|while循环的使用方法|99乘法口诀表倒着打印
- 用while循环做一个九九乘法表(用while循环和for循环分别输出九九乘法表)
- 怎么用三种代码写「九九乘法表」(九九乘法表的代码怎么写)
- 标签列表
-
- 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)