2013-11-28 16 views
0

我做了一個簡單的python加速計算器,但我遇到的問題是讓程序返回到開始或退出。我希望用戶能夠點擊'1'並重新啓動計算器或點擊'2'並讓程序退出。我似乎無法弄清楚如何運行我的主要功能後,程序已經通過公式,有什麼建議?謝謝。在Python中重新啓動我的加速計算器?

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個空格,而在else添加break

此外輸入接收一個字符串,而不是一個數字。

+0

'if restart =='1':'它應該工作,如果它是Python 2,7,則使用'raw_input'。 –

0

希望我已經理解了你的問題,並且我還建議你總是寫一個基本程序,然後轉向複雜的邏輯並繼續在你設置的平臺上構建。

所以我寫了一個基本的程序,將獲得用戶輸入決定計算或退出。如果計算出來,那麼它會從用戶那裏得到兩個數字,並且會做加法並返回以等待是退出還是繼續進行計算。

!在/ usr/bin中/ Python的

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() 

我已經測試的代碼和它的作品。