python - 根据用户输入进行计算
问题描述
请问我可以用什么代码在python中实现这样的事情:
我想首先获取用户输入,用户将在其中输入运算符和两个操作数,然后使用运算符计算两个操作数以给出答案。
代码的示例执行是:
Please enter your calculations in this order: + 3 3
Your answer is 6
解决方案
you can use this simple script to understand the operations
operand = input('enetr the + , - , * , / please: ')
num1 , num2 = input('enetr first and second number please: ').split() #note : enetr
#the first number then space and second number
num1 = int(num1)
num2 = int(num2)
if operand == '+':
print('your answer is ',num1 + num2)
if operand == '-':
print('your answer is ',num1 - num2)
if operand == '/':
print('your answer is ',num1 / num2)
if operand == '*':
print('your answer is ',num1 * num2)
推荐阅读
- sql - Postgresql 列引用不明确
- python - 关于tweepy的私信请求
- sql - SQL:导入和导出数据向导 64 位不显示 Excel 工作簿选项
- javascript - 使用 Firestore 进行学生评分
- xpath - 从具有相同值 Xpath 的更多文章的表中查找值
- multithreading - IBM XMS:会话范围多线程安全吗?
- java - 如何以编程方式将 securitySchema 广告到 swagger.yaml
- android - PictureInPicture模式Activity生命周期问题
- next.js - 如何使用路由器更改 NextJS 中的语言环境?
- grafana - 未知列“_source_measurement”infuxdbv2.1