由于某些原因,此代码不起作用?我已经尝试过返回1并中断,但由于某种原因,它给了我一个错误,我希望代码返回到开头,如果数字太长,但没有理想的如何去做。Python代码不能按预期工作
# Find the cube root of a perfect cube
x = int(input('Enter an integer: '))
if x > 5000:
break:
print('too long')
### this code is broken ^^^^^
ans = 0
while ans**3 < x:
ans = ans + 1
if ans**3 != x:
print(str(x) + ' is not a perfect cube')
else:
print('Cube root of ' + str(x) + ' is ' + str(ans))
IndentationError: unexpected indent
>>> runfile('/home/dux/pyyyyy.py', wdir=r'/home/dux')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/dux/pyyyyy.py", line 7
print('wrong'):
^
SyntaxError: invalid syntax
>>> runfile('/home/dux/pyyyyy.py', wdir=r'/home/dux')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/dux/pyyyyy.py", line 7
break:
^
SyntaxError: invalid syntax
>>> runfile('/home/dux/pyyyyy.py', wdir=r'/home/dux')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/dux/pyyyyy.py", line 8
print('wrong')
^
IndentationError: unexpected indent
>>> runfile('/home/dux/pyyyyy.py', wdir=r'/home/dux')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/dux/pyyyyy.py", line 7
break:
^
SyntaxError: invalid syntax
>>>
你确定你知道[什么'break'确实](http://docs.python.org/2/tutorial/controlflow.html)? – goncalopp
您是否为breakpoint或yield而产生了“break”? – ereOn