2017-03-18 35 views
0

我想弄清楚把這個功能分成兩個單獨的功能的最佳方法。一個是Main(),另一個是determineStatus()。我必須使用Main()來調用determinStatus。該代碼正是我想要它做的只是不確定一個有效的方式來分裂它。我想要把這個功能分解成兩個獨立的功能的最佳方式

不太確定一種分解方式而不會出現大量錯誤。

message="How many current credit hours do you have?" 

def determineStatus(message): 
    while True: 
    try: 
     userInput = int(input(message))  
    except ValueError: 
     print("Please use whole numbers only. Not text nor decimals.") 
     continue 
    else: 
     return userInput 

hours = determineStatus(message) 
F=30 
J=60 
S=90 
Max=200 

if hours <= Max: 
     if hours < F: 
     print("You are classified as a Freshman") 
    if hours > F and hours < J: 
     print("You are classified as a Sophmore") 
     if hours >= J and hours < S: 
     print("You are classified as a Junior") 
     if hours >= S and hours < Max: 
     print("You are classified as a Senior") 
else: 
    print("With",hours," hours you are either an Alumni, 2nd Degree seeking student or lying about your hours.") 

determineStatus(message) 
+0

只要把所有的外部語句中'高清的main():'然後調用'主()' – AChampion

+0

你是什麼意思? @克里斯馬丁 – Yrroth

回答

0

我會這樣做。

創建一個模塊

F = 30 
J = 60 
S = 90 
Max = 200 


def determineStatus(message): 
    while True: 
     try: 
      userInput = int(input(message)) 
     except ValueError: 
      print("Please use whole numbers only. Not text nor decimals.") 
      continue 
     else: 
      return userInput 


def calculateStatus(hours): 
    if hours <= Max: 
     if hours < F: 
      return "You are classified as a Freshman" 
    if hours > F and hours < J: 
     return "You are classified as a Sophmore" 
    if hours >= J and hours < S: 
     return "You are classified as a Junior" 
    if hours >= S and hours < Max: 
     return "You are classified as a Senior" 
    else: 
     return "With {0} hours you are either an Alumni, 2nd Degree seeking student or lying about your hours.".format(hours) 

現在創建一個小腳本:

import temp 

message = "How many current credit hours do you have?" 

# You can repeat the lines below by using a while loop 

hours = temp.determineStatus(message) 

print temp.calculateStatus(hours) 
+0

甚至不知道你改變了什麼。我只需要幫助將這一個功能分成兩個功能。 – Yrroth

+0

@CharlesGoodling我做了,我在模塊中創建了2個獨立的函數,我從腳本中調用它們。 – Elmex80s

+0

你的代碼甚至沒有運行。如果我應該做soem腳本,我對如何做到這一點一無所知,甚至爲什麼要解決這個問題 – Yrroth

2

右數據結構是一個偉大的代碼刀具。

# python 3.x 

CLASSIFIER = [ 
    # (min, max, status) 
    (0, 30, 'Freshman'), 
    (30, 60, 'Sophomore'), 
    (60, 90, 'Junior'), 
    (90, 200, 'Senior'), 
] 


def classify(hours): 
    assert hours >= 0, 'WTF, negative hours' 
    for (lower, upper, status) in CLASSIFIER: 
     if lower <= hours < upper: 
      return status 
    return 'Alumni, 2nd Degree seeking student or lying about your hours' 


def ask(message): 
    while True: 
     try: 
      return int(input(message)) 
     except ValueError: 
      print('Try entering a non-negative whole number again.') 


def main(): 
    hours = ask('How many hours? ') 
    print('With %d hours, you are %s' % (hours, classify(hours))) 

# Optional: auto-invoke main() if we're being executed as a script. 
if __name__ == '__main__': 
    main() 
+0

當試圖使用這種編碼時,它會爲每個打印語句提供語法錯誤。 – Yrroth

+0

運行測試時甚至不會輸出任何結果。 – Yrroth

+0

不太確定需要做些什麼才能讓它起作用,但它不會。 – Yrroth

0

對於您的多個if s,您將收到冗餘部門的警告!

如果小時數不小於J,則不需要檢查它是否大於或等於J

另外,如果hours = F,則會返回該學生說謊。

最後,你不會爲hours = Max返回任何東西。

這裏有一個優化的determine_status功能:

statuses = [ 
    (30, 'Freshman'), 
    (60, 'Sophomore'), 
    (90, 'Junior'), 
    (200, 'Senior') 
] 


def determine_status(hours): 
    for max_hours, status in statuses: 
     if hours < max_hours: 
      return "You are classified as a %s" % status 
    return "With %d hours you are either an Alumni, 2nd Degree seeking student or lying about your hours." % hours 

print(determine_status(0)) 
# You are classified as a Freshman 
print(determine_status(30)) 
# You are classified as a Sophomore 
print(determine_status(55)) 
# You are classified as a Sophomore 
print(determine_status(75)) 
# You are classified as a Junior 
print(determine_status(100)) 
# You are classified as a Senior 
print(determine_status(205)) 
# With 205 hours you are either an Alumni, 2nd Degree seeking student or 
# lying about your hours. 
+0

是的,我只允許在章或前幾章內做些事情,這使得它很棘手,因爲我知道很多我們還沒有討論過的東西。這就是爲什麼我要求專家協助。我有這個問題的概念,它只是想出一種方法來簡化它,而不會在本書的範圍之內。 – Yrroth

+0

@CharlesGoodling儘管我的評論仍然代表冗餘,J和max。 –

+0

我同意它是redunant,但仍然不能真正幫助我的問題。 – Yrroth