我想要把这个功能分解成两个独立的功能的最佳方式

问题描述:

我想弄清楚把这个功能分成两个单独的功能的最佳方法。一个是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

我会这样做。

创建一个模块

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

右数据结构是一个伟大的代码刀具。

# 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

对于您的多个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