【发布时间】:2016-11-07 06:00:53
【问题描述】:
print('Enter a mathematical expression: ')
expression = input()
space = expression.find(' ')
oprand1 = expression[0 : space]
oprand1 = int(oprand1)
op = expression.find('+' or '*' or '-' or '/')
oprand2 = expression[op + 1 : ]
oprand2 = int(oprand2)
if op == '+':
ans = int(oprand1) + int(oprand2)
print(ans)
假设用户输入 2 + 3,每个字符之间有一个空格。我如何让它打印 2 + 3 = 5?我需要代码来处理所有操作。
【问题讨论】:
-
你用的是哪个版本的python? stackoverflow.com/questions/1093322/…
-
Anaconda spyder
-
可以打印
import syssys.version的结果吗 -
您没有正确使用
find。提供的解决方案应该为您提供另一种方法。但是,如果您想尝试自己解决这个问题,请重新考虑您在find周围的工作。
标签: python string int find mathematical-expressions