首页 > 解决方案 > 根据用户输入进行计算

问题描述

请问我可以用什么代码在python中实现这样的事情:

我想首先获取用户输入,用户将在其中输入运算符和两个操作数,然后使用运算符计算两个操作数以给出答案。

代码的示例执行是:

Please enter your calculations in this order: + 3 3
Your answer is 6 

标签: pythoninput

解决方案


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)

推荐阅读