我想要把这个功能分解成两个独立的功能的最佳方式
问题描述:
我想弄清楚把这个功能分成两个单独的功能的最佳方法。一个是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)
答
我会这样做。
创建一个模块
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)
答
右数据结构是一个伟大的代码刀具。
# 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()
答
对于您的多个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.
只要把所有的外部语句中'高清的main():'然后调用'主()' – AChampion
你是什么意思? @克里斯马丁 – Yrroth