我正在试图找出类似“输入表达式:”的内容如何接受3个变量:第一个int,操作的字符和第二个int。只需cin>>就可以在C ++中轻松实现这一点。 num1>>操作>> NUM2。
到目前为止,根据其他人的问题,我已经尝试过列入并拆分它。这有效,除了超过1位的整数。我正在做这样的事情:
list1=raw_input()
list1.split()
print list1
num1=list1[0]
plus=list1[1]
num2=list1[2]
print num1, plus, num2
例如,输入10 + 3将输出1 0 + 我觉得这里有一个简单的解决方法,但我不知道。任何帮助表示赞赏。
答案 0 :(得分:1)
字符串是不可变的,因此您需要在某处捕获list1.split()
的结果。但它不会帮助你,因为那不会做你想要的。使用解析器,可能使用Python's language services。
答案 1 :(得分:1)
请改为尝试:
list1 = raw_input()
for x in list1.split():
print x,
答案 2 :(得分:0)
我建议在这种情况下使用正则表达式,例如:
re_exp = re.compile(r'\s*(\d+)\s*([^\d\s])+\s*(\d+)')
expr = raw_input()
match = re_exp.match(expr)
if match:
num1, oper, num2 = match.groups()
print num1, oper, num2
使用split
,您可以解析10 + 1
但是使用10+1
(没有空格)会更难处理,或处理这两种情况。
答案 3 :(得分:0)
#You should write like this
list1 = raw_input()
a=list1.split()
num1=a[0]
plus=a[1]
num2=a[2]
print num1, plus, num2