Python 从用户进行计算';s输入

Python 从用户进行计算';s输入,python,input,Python,Input,请确定我可以使用什么代码在python中实现类似的功能: 我想首先获得用户输入,用户将在其中输入运算符和两个操作数,然后使用运算符计算两个操作数以给出答案 代码的示例执行是: Please enter your calculations in this order: + 3 3 Your answer is 6 将两个操作数作为整数,使用if语句或switch检查运算符,并基于该运算符执行类似if运算符为“+”的操作,并且在语句中发现为true,然后将这些操作数添加到其他操作数中。),依此类

请确定我可以使用什么代码在python中实现类似的功能:

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

代码的示例执行是:

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

将两个操作数作为整数,使用if语句或switch检查运算符,并基于该运算符执行类似if运算符为“+”的操作,并且在语句中发现为true,然后将这些操作数添加到其他操作数中。

),依此类推……

我假设您需要一个形式为“(操作数)的单一输入(第一个数字)(第二个数字)”。在这种情况下,首先,您需要使用Arun K建议的拆分函数。然后,您需要将数字从字符串转换为整数,然后将运算符与预设的运算符列表进行比较。代码可能如下所示:

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)
problem = input("Enter 2 operands and operator divided by space (e.g. 3 3 +): ")
a,b,operator = problem.split(" ")
a = int(a)
b = int(b)
if operator == "+":
  c = a+b
elif operator == "*":
  c = a*b
# more operators here, if required
print('Result: {}'.format(c))

如果你真的想彻底了解,那么你可以使用try/except语句来确保输入是正确的。

提示:a,b,c=input().rsplit(),欢迎使用SO。始终尝试显示你迄今为止尝试过的内容