ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Oct 2015 23:09:54 -0500Can't find Cause of Traceback Errorhttp://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/ I'm trying to convert this code that runs in Python 2.7 to Sage but can't even get a hint from Sage at what line the code is not accepted by Sage.
def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)]
crev = [] ''' Calculating Cube Roots'''
for x,x3 in enumerate(cubes): crev[x3] = x + 1
sums = sorted(x + y for x in cubes for y in cubes if y < x) ''' Organizing Data'''
for i in range(1, len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
if sums[i]<=t: ''' Limiting how many solutions printed.'''
print "%10d"%(sums[i]) ''' Printing desired outputs '''
else:
break ''' Ending the function.'''
The only thing that Sage will report back is Traceback Error for SyntaxError: invalid syntax but no line information..
Can anyone tell where specifically the syntax is invalid??Sun, 11 Oct 2015 22:40:23 -0500http://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/Comment by fidbc for <p>I'm trying to convert this code that runs in Python 2.7 to Sage but can't even get a hint from Sage at what line the code is not accepted by Sage.</p>
<pre><code>def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)]
crev = [] ''' Calculating Cube Roots'''
for x,x3 in enumerate(cubes): crev[x3] = x + 1
sums = sorted(x + y for x in cubes for y in cubes if y < x) ''' Organizing Data'''
for i in range(1, len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
if sums[i]<=t: ''' Limiting how many solutions printed.'''
print "%10d"%(sums[i]) ''' Printing desired outputs '''
else:
break ''' Ending the function.'''
</code></pre>
<p>The only thing that Sage will report back is Traceback Error for SyntaxError: invalid syntax but no line information..
Can anyone tell where specifically the syntax is invalid??</p>
http://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/?comment=29925#post-id-29925This code does not seem to run in Python 2.7, same error as reported by sage.
Traceback (most recent call last): if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
File "", line 1, in <module>
File "/tmp/tmp_QhfdJ/___code___.py", line 5
crev = [] ''' Calculating Cube Roots'''
^
SyntaxError: invalid syntaxSun, 11 Oct 2015 23:09:54 -0500http://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/?comment=29925#post-id-29925Answer by fidbc for <p>I'm trying to convert this code that runs in Python 2.7 to Sage but can't even get a hint from Sage at what line the code is not accepted by Sage.</p>
<pre><code>def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)]
crev = [] ''' Calculating Cube Roots'''
for x,x3 in enumerate(cubes): crev[x3] = x + 1
sums = sorted(x + y for x in cubes for y in cubes if y < x) ''' Organizing Data'''
for i in range(1, len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
if sums[i]<=t: ''' Limiting how many solutions printed.'''
print "%10d"%(sums[i]) ''' Printing desired outputs '''
else:
break ''' Ending the function.'''
</code></pre>
<p>The only thing that Sage will report back is Traceback Error for SyntaxError: invalid syntax but no line information..
Can anyone tell where specifically the syntax is invalid??</p>
http://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/?answer=29924#post-id-29924 It seems like you are working in the notebook environment in sage. Perhaps you are getting the following output
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
And if you click to the left of the block (as indicated above) you should see something like
Traceback (most recent call last): if sums[i-1] != sums[i] and sums[i] == sums[i+1]: ''' Finding solutions'''
File "", line 1, in <module>
File "/tmp/tmp_QhfdJ/___code___.py", line 5
crev = [] ''' Calculating Cube Roots'''
^
SyntaxError: invalid syntax
It seems that the trouble is being caused by the use of triple quotes, the hash character (`#`) is recommended for comments.
def Ramanujan(t):
cubes = [x**3 for x in range(1,t/10)]
crev = [] # Calculating Cube Roots
for x,x3 in enumerate(cubes):
crev[x3] = x + 1
sums = sorted(x + y for x in cubes for y in cubes if y < x) # Organizing Data
for i in range(1, len(sums)-1):
if sums[i-1] != sums[i] and sums[i] == sums[i+1]: # Finding solutions
if sums[i]<=t: # Limiting how many solutions printed.
print "%10d"%(sums[i]) # Printing desired outputs
else:
break # Ending the function.
The code seems to be broken, `Ramanujan(20)` yields an error.Sun, 11 Oct 2015 23:05:54 -0500http://ask.sagemath.org/question/29923/cant-find-cause-of-traceback-error/?answer=29924#post-id-29924