我试图找出将此功能分成两个独立功能的最佳方法。一个是Main(),另一个是determineStatus()。我必须使用Main()来调用determineStatus。代码完全符合我的要求,只是不确定分割它的有效方法。
不确定是否有办法将其拆分而不会出现大量错误。
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 :(得分:2)
正确的数据结构是一个很好的代码切割工具。
if(Input::exists()){
if(Token::check(Input::get('token'))){
$validate = new Validate();
$validation = $validate->check($_POST, array(
'id' => array('required' => 'true'),
'activecode' => array('required' => 'true')
));
if($validation->passed()){
$user = new User();
$user->active(array(
'active' => 1
));
Session::flash('success', 'success.');
Redirect::to('index.php');
}else{
echo "error";
}
}else{
pre($validation->errors());
}
}`
答案 1 :(得分: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)
答案 2 :(得分:0)
对于您的多个if
,您会收到冗余部门的警告!
如果小时数不小于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.