2013-11-28 1 views
0

나는 파이썬 가속 계산기를 만들었지 만 문제는 프로그램이 처음이나 종료로 돌아가는 것입니다. 사용자가 '1'을 누르고 계산기를 다시 시작하거나 '2'를 누르고 프로그램을 종료 시키길 원합니다. 프로그램이 방정식을 통과 한 후에 내 주요 기능을 실행하는 방법을 알아낼 수 없습니다. 감사. 귀하의 들여 쓰기가 잘못내 가속 계산기를 파이썬으로 다시 시작 하시겠습니까?

class equation(): 

    def main(): 
     Calc() 
     while True: 
      restart = input("Would you like to run again? If yes press '1' if you wish to Exit press '2'") 
      if restart==1: 
       Calc() 
      else: 
       print "Goodbye" 
       break 

    print "What is your Velocity: " 
    v = float(input()) #m/s 
    print "What is your Intial Velocity: " 
    u = float(input()) #m/s 
    print "What is the time: " 
    t = float(input()) #Seconds 
    aa = v-u 
    answer = aa/t 

eq=equation() 

print "Your Acceleration is: ", eq.answer, "m/s^2" 

print eq.main 

답변

0

, 전체 if 4 개 공간 들여 쓰기, 그리고 elsebreak를 추가해야합니다.

또한 입력은 숫자가 아닌 문자열을 수신합니다.

+0

'restart == '1':'그러면 작동하고 파이썬 2,7이면 raw_input을 사용합니다. –

0

희망 사항 귀하의 질문을 이해했으며 기본 프로그램을 작성한 다음 복잡한 논리로 이동하고 설정 한 플랫폼을 계속 구축 할 것을 제안합니다.

그래서 계산하거나 종료하기로 결정하는 사용자 입력을 얻는 기본 프로그램을 작성했습니다. 계산중인 경우 사용자로부터 두 개의 숫자를 가져 와서 더하기를 수행하고 종료하거나 계산을 계속할 때까지 기다릴 수 있습니다.

!는/usr/빈/파이썬은

class equation: 


     def calculate(self): 
       print 'Enter the first number' 
       input1 = int(raw_input()) 
       print 'Enter the second number' 
       input2 = int(raw_input()) 
       result = input1 + input2 
       print result 

     def mainfun(self): 
       while True: 
         print 'Enter 1 for calculate operation and any to exit' 
         userInput = str(raw_input()) 
         if userInput=='1': 
           self.calculate() 
         else: 
           print 'Good bye' 
           break 

operation = equation() 

operation.mainfun() 

나는 코드를 테스트 한 작동합니다.

관련 문제