0

以下是我在Python 2.7中構建的一個簡單的Mils to Degrees轉換計算器的(半)完成版本,作爲學習練習。我是Python新手,仍然在努力解決問題。前提是,用戶選擇將mils轉換成度,反之亦然。用戶輸入所選單位的方位角並將其轉換爲另一個單位。如果用戶選擇一個無效的菜單選項,他會被通知。如果他輸入範圍之外的方位角(1-360度,密耳爲1-6400),他將被通知並且必須重新開始。問題是,如果用戶輸入一個無效的方位角,比如365度,他會被踢回菜單。 有沒有辦法返回到if/else循環中的上一步?我搜索了論壇和文檔,它似乎不可能。另外,由於我是Python的新手,如何使此代碼更高效? 我是否需要如果elif語句在函數定義中,還是可以將它們組合在一起而不會產生錯誤或冗餘輸出?我沒有成功這樣做。所有的投入都歡迎和讚賞。Python轉換計算器循環/代碼效率

#Mil-Deg.py 
#Simple Mils/Degrees Conversion Calculator 
#6 JAN 2013 

#Note: 6400 mils/360 degrees in a circle. 17.78 mils to one degree. 

import sys 
import math 
import winsound 

#Define menu function 
def menu():      

    print 
    print " Mils/Degrees Conversion Calculator" 
    print "-" * 38 
    print 
    print "Options: " 
    print "1. Degrees to Mils" 
    print 
    print "2. Mils to Degrees" 
    print 
    print "3. Quit" 
    print "-" * 20 
    print 
    return input ("Choose your option: ") 
    print 

#Define mils to degrees function  
def m2d(a): 
    if a <= 6400 and a >= 1: #get user input within given range  
     b = 17.78 
     c = round((a/b),0) #convert and round to nearest degree 
     if c > 359 or c < 1: #change 0 degrees to 360    
      c = 360 
      #Output 
      print 
      print a, "mils =", int (c), "degrees"  
     else: 
      print 
      print a, "mils =", int (c), "degrees" 
    elif a > -1 and a < 1:  #change 0 mils to 6400 
     a = 6400 
     b = 17.78 
     c = round((a/b), 0) #math, same as above 
     if c > 359 or c < 1: #0 to 360, same as above    
      c = 360 
      #Output 
      print 
      print 6400, "mils =", int (c), "degrees" 
     else: 
      print 
      print a, "mils =", int (c), "degrees" 
    else: 
     #warning, mulligan 
     winsound.Beep(440, 500) 
     print 
     print "*** There are only 6400 mils in a circle. Try again. ***" 
    print 
    print "-" * 38 
    print 

#define degrees to mils function   
def d2m(b): 
    if b <= 360 and b > 0:   #get user input within given range 
     a = 17.78 
     c = round((b * a),0)  #convert and round to nearest mil 
     if c >= 6400:    #set limit to 6400 mils 
      c = 6400 
      #Output 
      print 
      print b, "degrees =", int (c), "mils" 
     else: 
      print 
      print b, "degrees =", int (c), "mils" 
    elif b > -1 and b < 1:   #change 0 to 360 degrees 
     b = 360 
     a = 17.78 
     c = round((b * a),0)  #math, same as above 
     if c >= 6400: 
      c = 6400 
      #Output 
      print 
      print 360, "degrees =", int (c), "mils" 
     else: 
      print 
      print b, "degrees =", int (c), "mils" 
    else: 
     #warning 
     winsound.Beep(440, 500) 
     print 
     print "*** There are only 360 degrees in a circle. Try again. ***" 
    print 
    print "-" * 38 
    print 


#Begin program 
loop = 1 
choice = 0 
while loop == 1: 
    choice = menu()     #Menu function call 
    if choice == 1:     #If user chooses degrees to mils: 
     #Output 
     print 
     print "Enter your azimuth in degrees (1 - 360)" 
     print 
     d2m(input("Degrees: "))  #function call 

    elif choice == 2:    #If user chooses mils to degrees: 
     #Output 
     print 
     print "Enter your azimuth in mils (1 - 6400)" 
     print 
     m2d(input("Mils: "))  #function call 

    elif choice == 3:    #If user chooses quit, exit program 
     loop = 0 

    elif choice != 1 and choice != 2 and choice != 3: #if user makes invalid menu choice: 
     #warning 
     winsound.Beep(440, 500) 
     print 
     print 
     print 
     print choice, "is not a valid choice." 
     print 
     print "Please choose from the available options." 
     print 
     print 

print 
print "-" * 38 
print 
print "Thank you for using Mil-Deg.py" 
print 
print 
+0

'如果-else'不是一個循環,嘗試'while'循環這裏。 –

+0

你的'return'語句後面有'print',它永遠不會被執行。你還應該使用'raw_input',它比'input'安全得多。 'raw_input'將接受輸入並將其轉換爲一個字符串。 –

+0

@AshwiniChaudhary,謝謝。我有一點C經驗。我應該意識到這一點。我嘗試過,但我把它放在函數定義而不是執行塊,它導致了一個無限循環。現在,我把它放在正確的地方,它按預期工作。謝謝。 – Betastate

回答

0

我清理你的代碼了一下,做了以下修改:

  1. 固定打印報表
  2. 固定的方法名。方法名稱應該是描述性的。字符可以自由輸入,所以不要小心輸入完整的單詞。
  3. 你的方法是打印輸出和複製邏輯,所以我清理它們只是返回值而已。這樣你可以在以後的其他程序中使用這些方法。
  4. 添加了一個輔助方法來檢查用戶的輸入(再次停止重複/複製粘貼相同的代碼)。
  5. 我使用了字符串的format函數,以便於打印計算。

下面是結果:

import sys 
import math 
import winsound 

#Define menu function 
def menu():      
    '''This function prints the main menu''' # this is a docstring for your method 
    print "\nMils/Degrees Conversion Calculator" 
    print "-" * 38 
    print "\nOptions: " 
    print "1. Degrees to Mils\n" 
    print "2. Mils to Degrees\n" 
    print "3. Quit\n" 
    print "-" * 20 
    print "\n" 


#Define mils to degrees function  
def mils_to_degrees(a): # use method names that make sense 
    '''This function takes mils and converts them into degrees''' 
    b = 17.78 
    c = round((a/b),0) #convert and round to nearest degree 
    if c > 359 or c < 1: #change 0 degrees to 360    
     c = 360 
    return (a,int(c)) 

#define degrees to mils function   
def degrees_to_mils(b): 
    '''This method converts degrees to mils''' 
    a = 17.78 
    if b == 0: 
     b = 360 
    c = round((b * a),0)  #math, same as above 
    if c > 6400:    #set limit to 6400 mils 
     c = 6400 
    return (b,c) 

def check_input(user_input): 
    '''Checks if the input is a number''' 
    try: 
     converted_number = int(user_input) 
    except ValueError: 
     return -1 
    return converted_number 

#Begin program 
loop = True 
while loop: 
    menu() 
    choice = int(raw_input("Choose your option: ")) 
    if choice not in [1,2,3]: 
     winsound.Beep(440, 500) 
     print choice, "is not a valid choice.\n" 
     print "Please choose from the available options.\n" 
     menu() 
     choice = int(raw_input('Choose your option: ')) 

    if choice == 1: 
     print "\nEnter your azimuth in degrees (0 - 360)\n" 
     degrees = check_input(raw_input('Degrees: ')) 
     if degrees > 360 or degrees < 0: 
      print 'Please enter a value between 0 and 360' 
     else: 
      result = degrees_to_mils(degrees) # function call 
      print '{} degrees = {} mils'.format(*result) 
      print "-" * 38 

    elif choice == 2:    #If user chooses mils to degrees: 
     print "\nEnter your azimuth in mils (1 - 6400)\n" 
     mils = check_input(raw_input('Mils: ')) 
     if mils > 6400 or mils < 1: 
      print 'Please enter a value between 1 and 6400' 
     else: 
      result = mils_to_degrees(mils)  #function call 
      print '{} mils = {} degrees'.format(*result) 
      print "-" * 38 

    elif choice == 3: 
     loop = False 

print "\n" 
print "-" * 38 
print "\nThank you for using Mil-Deg.py\n\n" 
+0

哇。這是對我原來的代碼的巨大改進。我相信這對你來說很容易,但你已經給了我很多東西來學習和吸收。非常感謝,夥計。我非常感謝幫助! – Betastate

0

由於阿什維尼在評論中提到,你if-else條件塊並不是一個真正的循環,所以用while循環應該回答你的問題的程序流程的一部分。

就elif的和普通的優雅而言,可以考慮this method of handling multiple options using a dictionary of functions;在這裏少數選項當然不是必需的,但它可能會給你一些練習Python如何處理字典和函數的類。

在風格上,我建議使用換行符轉義序列\n而不是所有這些無關的打印語句。

+0

卡梅隆,while循環確實解決了上述問題。我仍在處理其餘的建議。我需要再讀幾遍字典。感謝您的建議。我很感激。 – Betastate