tags:

views:

360

answers:

5

my input is something like this

23 + 45 = astart

for the exact input when i take it as raw_input() and then try to split it , it gives me an error like this

SyntaxError: invalid syntax

the code is this

k=raw_input()
a,b=(str(i) for i in k.split('  +   '))
b,c=(str(i) for i in b.split('  =   '))

its always number + number = astar

its just that when i give number+number=astar i am not getting syntax error ..!! but when i give whitespace i get sytax error

+1  A: 
cobbal
still syntax error
mekasperasky
-1. Generators can be unpacked just fine.Try a,b = (i for i in (1,2)) for confirmatino.
Triptych
+2  A: 

Testing with Python 2.5.2, your code ran OK as long as I only had the same spacing on either side of the + and = in the code and input.

You appear to have two spaces on either side of them in the code, but only one on either side in the input. Also - you do not have to use the str(i) in a generator. You can do it like a,b=k.split(' + ')

My cut and pastes:

My test script:

print 'Enter input #1:'
k=raw_input()

a,b=(str(i) for i in k.split(' + '))
b,c=(str(i) for i in b.split(' = '))

print 'Here are the resulting values:'
print a
print b
print c


print 'Enter input #2:'
k=raw_input()

a,b=k.split(' + ')
b,c=b.split(' = ')

print 'Here are the resulting values:'
print a
print b
print c


From the interpreter:

>>> 
Enter input #1:
23 + 45 = astart
Here are the resulting values:
23
45
astart
Enter input #2:
23 + 45 = astart
Here are the resulting values:
23
45
astart
>>>
Anon
Works fine here for me too. And if i take out the spaces, i get ValueError, not syntax error. If it was me, i would just do "a, b = (i for i in k.split('+')", without spaces.
lyrae
+1  A: 

I think I'd just use a simple regular expression:

# Set up a few regular expressions
parser = re.compile("(\d+)\+(\d+)=(.+)")
spaces = re.compile("\s+")

# Grab input
input = raw_input()

# Remove all whitespace
input = spaces.sub('',input)

# Parse away
num1, num2, result = m.match(input)
Triptych
+1  A: 

You could just use:

a, b, c = raw_input().replace('+',' ').replace('=', ' ').split()

Or [Edited to add] - here's another one that avoids creating the extra intermediate strings:

a, b, c = raw_input().split()[::2]

Hrm - just realized that second one requires spaces, though, so not as good.

Anon
A: 

Rather than trying to solve your problem, I thought I'd point out a basic step you could take to try to understand why you're getting a syntax error: print your intermediate products.

k=raw_input()
print k.split('  +   ')
a,b=(str(i) for i in k.split('  +   '))
print b.split('  =   ')
b,c=(str(i) for i in b.split('  =   '))

This will show you the actual list elements produced by the split, which might shed some light on the problem you're having.

I'm not normally a fan of debugging by print statement, but one of the advantages that Python has is that it's so easy to fire up the interpreter and just mess around interactively, one statement at a time, to see what's going on.

Vicki Laidler