2014-07-14 2 views
0

방금 ​​창을 닫으려면 실행하자 마자 첫 번째 Python 프로그램을 작성했습니다. 나는 행운이없는 다른 스레드에서 발견 된 몇 가지 픽스를 시도했다. 다음은 코드입니다. 구문에서 오류가 발생합니다. 도와 주셔서 감사합니다!실행했을 때 즉시 프로그램이 닫힙니다.

print ('Lets do some math nerd!') 
print ('How many problems would you like to solve?') 
cycle = int(input()) 
rep = 0 
while cycle < rep; 
rep = rep + 1; 

print ('What operation would you like to perform?') 
print('Press 1 for Addition') 
print('Press 2 for Subtraction') 
print('Press 3 for Multiplication') 
print('Press 4 for Division') 

op = int(input()) 

if op ==1 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 + num2) 
    print result 

elif op ==2 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 - num2) 
    print result 

elif op ==3 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 * num2) 
    print result 

elif op ==4 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1/num2) 
    print result 

else  
    print ('ERROR') 
    print (Please select a number 1 through 4.') 
print ('Thanks for using my calculator!') 

closeInput = raw_input("Press ENTER to exit") 
print "Closing..." 
+2

그냥 대신 클릭의 콘솔에서 코드를 실행이. 창은 사라지지 않을 것입니다, 당신은 예외를 볼 수있을 것이다하시기 바랍니다 –

+1

이것은 올바르지 않을 수 있습니다 :'while cycle

+0

어떻게 실행합니까? Windows 시스템을 사용하는 경우 , 그리고 파이썬 파일을 더블 클릭하면 그 일이 일어날 것입니다. 대신 명령 프롬프트를 열고'python myfile.py'을 실행하십시오 – karthikr

답변

0

코드에 구문 오류가 있습니다.

바로 가기 또는 시작 프로그램에서 프로그램을 시작한다고 가정합니다. 따라서 추적 추적 파이썬이 일반적으로 제공하는 오류를 볼 수 없습니다.

대신 콘솔에서 프로그램을 실행하십시오. 예 : python myprogram.py 또는 python3 myprogram.py. 이것은 프로그램을 죽이는 오류에 대한 추적을 제공합니다. 코드에서

오류 :

  • 모든 경우/ELSEIF 라인은 콜론으로 끝나야 동안/다른/
  • 라인 (51)에 문자열이 시작 따옴표에게 잠시 아래
  • 모든 것을해야한다 (콜론으로 끝나야한다) 라인은 들여 쓰기를한다
  • (안 구문 오류)는 arround를하지 다른 방법
, rep < cycle 확인해야 6,

이 고정 버전 :

#!/usr/bin/python 
print ('Lets do some math nerd!') 
print ('How many problems would you like to solve?') 
cycle = int(input()) 
rep = 0 
while rep < cycle: 
    rep = rep + 1 

    print ('What operation would you like to perform?') 
    print('Press 1 for Addition') 
    print('Press 2 for Subtraction') 
    print('Press 3 for Multiplication') 
    print('Press 4 for Division') 

    op = int(input()) 

    if op ==1: 
     print ('Enter your first number.') 
     num1 = int(input()) 
     print ('Enter your second number.') 
     num2 = int(input()) 
     result = (num1 + num2) 
     print result 

    elif op ==2: 
     print ('Enter your first number.') 
     num1 = int(input()) 
     print ('Enter your second number.') 
     num2 = int(input()) 
     result = (num1 - num2) 
     print result 

    elif op ==3: 
     print ('Enter your first number.') 
     num1 = int(input()) 
     print ('Enter your second number.') 
     num2 = int(input()) 
     result = (num1 * num2) 
     print result 

    elif op ==4: 
     print ('Enter your first number.') 
     num1 = int(input()) 
     print ('Enter your second number.') 
     num2 = int(input()) 
     result = (num1/num2) 
     print result 

    else: 
     print ('ERROR') 
     print ('Please select a number 1 through 4.') 

print ('Thanks for using my calculator!') 

closeInput = raw_input("Press ENTER to exit") 
print "Closing..." 
+0

감사합니다. 지금은 작동하지 않는 것 같지만 콘솔에서 실행되도록 할 수는 있지만 더 많은 문제 해결이 필요하다고 생각합니다. –

+0

사용중인 운영 체제는 무엇이며 사용하는 명령은 무엇입니까? 명령을 입력하면 콘솔에서 무엇을 말합니까? – Oleg

+0

나는 명령 행에있을 때 7시에 나는 Python을 C : \ python27 \ python exe라고 입력하여 실행할 수있다. 그러나 Python을 입력하면 정의되지 않은 오류가 발생하여 실행되지 않는다. –

1

당신은 다음, 첫 번째 줄에 "수입 PDB"를 추가 실행하고 당신이 당신의 프로그램을 디버깅 도움이 될 수 있습니다.

몇 가지 문법 오류 : 1. 줄 7, while 루프는 ";"대신 ";"을 사용합니다. 2. Line 8, IndentationError : 들여 쓰기 블록이 예상 됨 3. 18 번째 줄, op == 1 : 인 경우 모든 if 문과 동일합니다. 4 선 (52), 인쇄 ('

완료.

import pdb 

print ('Lets do some math nerd!') 
print ('How many problems would you like to solve?') 
cycle = int(input()) 
rep = 0 
while cycle < rep: 
     rep = rep + 1 

print ('What operation would you like to perform?') 
print('Press 1 for Addition') 
print('Press 2 for Subtraction') 
print('Press 3 for Multiplication') 
print('Press 4 for Division') 

op = int(input()) 

if op ==1: 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 + num2) 
    print result 

elif op ==2: 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 - num2) 
    print result 

elif op ==3: 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1 * num2) 
    print result 

elif op ==4: 
    print ('Enter your first number.') 
    num1 = int(input()) 
    print ('Enter your second number.') 
    num2 = int(input()) 
    result = (num1/num2) 
    print result 

else: 
    print ('ERROR') 
    print ('Please select a number 1 through 4.') 
print ('Thanks for using my calculator!') 

closeInput = raw_input("Press ENTER to exit") 
print "Closing..." 
+0

의견을 보내 주셔서 감사합니다. 그러나 제가 처음으로 알고있는 것보다 더 많은 문제가있는 것 같습니다. 그러나 지금은 작동 중입니다! –

관련 문제