2014-02-13 5 views
0

어떤 이유로이 코드가 작동하지 않습니까? 나는 return 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 
>>> 
+0

[무엇이'중단 '입니까?] (http://docs.python.org/2/tutorial/controlflow.html)? – goncalopp

+0

중단 점이나 'yield'에'break'를 혼동하고 있습니까? – ereOn

답변

0

중단으로 인해 루프가 중지됩니다. 코드가 루프에 없기 때문에 왜 코드를 사용해야하는지 알 수 없습니다. 또한 중단 후 콜론이 필요하지 않습니다. 그냥 휴식 시간을 알면 예를 들어 보겠습니다. 물론

count = 0 
while True: 
    print('Hello') #Prints Hello 
    if count == 20: #Checks if count is equal to 20 
     break #If it is: break the loop 
    count += 1 #Add 1 to count 

이 그냥 while count < 20:을하고 있지만이 점을 설명하고있다 의해 쉽게 수행 할 수있다.

편집 : 수신 한 다른 오류 중 일부를보고 print 뒤에 콜론이 필요하지 않습니다.

3

여기 사용자가 올바른 번호를 입력했는지 확인하는 것이 좋습니다. 시도해보십시오.

while True: 
    x = int(input('Enter an integer: ')) 
    if x > 5000: 
     print('too long') 
    else: 
     break 
관련 문제