設計一個多層決策系統來決定獎學金的資格標準。

WBOY
發布: 2024-07-17 06:02:49
原創
207 人瀏覽過

Design a multi-tier decision-making system to determine the eligibility criteria for a scholarship.

問題

使用 if-else 語句設計一個多層決策系統,可用於確定獎學金的資格標準。該系統應考慮多種因素,例如成績、課外活動和財務需求。

思維過程

要設計一個多層決策系統來根據成績、課外活動和財務需求確定獎學金資格,您需要一種結構化的方法來評估和分配獎學金金額。以下是如何使用 if-else 語句建立決策系統的逐步細分:

獎學金決定標準

  • 財務狀況:確定學生的經濟困難程度。
  • 課外活動:衡量學術以外的參與程度。
  • 成績:反映學業表現。

獎學金決定邏輯

  • 確定財務需求:檢查學生是否屬於高、中或低財務需求類別。
  • 評估課外活動參與:在每個經濟需求類別中,檢視學生在課外活動中的活躍程度。
  • 評估成績:最後,評估學生的成績以決定確切的獎學金金額。

獎學金金額

  • 高需求、高課外活動、高成績:最高獎學金。
  • 高需求,中課外活動,中等成績:中等獎學金。
  • 高需求,低課外活動,低成績:獎學金很少。
  • 中等需求,高課外活動,高成績:大量獎學金。
  • 中等需要,中等課外活動,中等成績:中等獎學金。
  • 中等需求,低課外活動,低成績:獎學金很少。
  • 低需求,高課外活動,高成績:中等獎學金。
  • 低需求,中課外活動,中等成績:小額獎學金。
  • 低需求、低課外活動、低成績:無獎學金。

解決方案

def scholarship(financial_cond, extra_act, grades):
    if financial_cond >=80:
        if extra_act >=80:
            if grades >=80:
                return 5000
            if grades>= 60 and grades <80:
                return 3000
            if grades>= 40 and grades <60:
                return 2000
        if extra_act >= 60 and extra_act < 80:
            if grades >=80:
                return 4000
            if grades>= 60 and grades <80:
                return 2000
            if grades>= 40 and grades <60:
                return 1000
        if extra_act >= 40 and extra_act <60:
            if grades >=80:
                return 3000
            if grades>= 60 and grades <80:
                return 1000
            if grades>= 40 and grades <60:
                return 000
    if financial_cond >=60 and financial_cond <80:
        if extra_act >=80:
            if grades >=80:
                return 4000
            if grades>= 60 and grades <80:
                return 2000
            if grades>= 40 and grades <60:
                return 1000
        if extra_act >= 60 and extra_act < 80:
            if grades >=80:
                return 3000
            if grades>= 60 and grades <80:
                return 1000
            if grades>= 40 and grades <60:
                return 0000
        if extra_act >= 40 and extra_act <60:
            if grades >=80:
                return 2000
            if grades>= 60 and grades <80:
                return 0000
            if grades>= 40 and grades <60:
                return 000
    if financial_cond >= 40 and financial_cond <60:
        if extra_act >=80:
            if grades >=80:
                return 3000
            if grades>= 60 and grades <80:
                return 1000
            if grades>= 40 and grades <60:
                return 000
        if extra_act >= 60 and extra_act < 80:
            if grades >=80:
                return 2000
            if grades>= 60 and grades <80:
                return 000
            if grades>= 40 and grades <60:
                return 000
        if extra_act >= 40 and extra_act <60:
            if grades >=80:
                return 1000
            if grades>= 60 and grades <80:
                return 000
            if grades>= 40 and grades <60:
                return 000
    else:
        return 0

financial_cond = float(input('Enter financial condition in 1 to 100 '))
extrac_act = float(input('Enter the extracurricular participation in 1 to 100 '))
grades= float(input('Enter the grades of the student '))

print(scholarship(financial_cond, extrac_act, grades))
登入後複製

其他想法

1

financial_cond = float(input('Enter financial condition in 1 to 100 '))
extrac_act = float(input('Enter the extracurricular participation in 1 to 100 '))
grades= float(input('Enter the grades of the student '))
def scholarship():
    if financial_cond >= 80 and extrac_act >= 80 and grades >= 80:
        return 10000
    elif financial_cond >= 80 and extrac_act >= 80 and grades >= 60 and grades <80:
        return 5000
    elif financial_cond >= 80 and extrac_act >= 80 and grades >= 40 and grades < 60:
        return 2000
    elif financial_cond >= 80 and extrac_act >= 60 and extrac_act < 80 and grades >= 80:
        return 5000
    elif financial_cond >=80 and extrac_act >=60 and extrac_act < 80 and grades >= 60 and grades < 80:
        return 2000
    elif financial_cond >= 80 and extrac_act >=50 and extrac_act < 60 and grades >= 60 and grades < 80:
        return 1000
    elif financial_cond >= 60 and financial_cond < 80 and extrac_act >= 80 and grades >=80:
        return 5000
    elif financial_cond >=60 and financial_cond < 80 and extrac_act >=60 and extrac_act < 80 and grades >= 80:
        return 2000
    elif financial_cond >=60 and financial_cond < 80 and extrac_act >= 60 and extrac_act < 80 and grades >= 60 and grades < 80:
        return 1000
    else:
        return 0

scholarship()
登入後複製

拒絕原因:

  1. 範圍與冗餘:scholarship() 函數不含參數,因此它依賴直接輸入的全域變數(financial_cond、extrac_act、grades)。這種設計模組化程度較低,可能會導致程式碼維護和測試方面出現問題。
  2. 重疊和歧義:某些條件重疊或冗餘。例如,多個 elif 分支檢查相似的條件,但閾值或範圍略有不同,這可能會使邏輯混亂且容易出錯。
  3. 邏輯複雜性:使用具有重疊條件的多個 elif 語句會建立難以理解的複雜結構。目前尚不清楚哪種情況為真,以及在各種情況下最終結果是什麼。

2

def scholarship(financial_cond, extra_act, grades):
    if financial_cond >= 80:
        return high_financial(extra_act, grades)
    elif financial_cond >= 60:
        return medium_financial(extra_act, grades)
    elif financial_cond >= 40:
        return low_financial(extra_act, grades)
    else:
        return 0

def high_financial(extra_act, grades):
    if extra_act >= 80:
        if grades >= 80:
            return 5000
        elif grades >= 60:
            return 3000
        elif grades >= 40:
            return 2000
    elif extra_act >= 60:
        if grades >= 80:
            return 4000
        elif grades >= 60:
            return 2000
        elif grades >= 40:
            return 1000
    elif extra_act >= 40:
        if grades >= 80:
            return 3000
        elif grades >= 60:
            return 1000
        elif grades >= 40:
            return 0

def medium_financial(extra_act, grades):
    if extra_act >= 80:
        if grades >= 80:
            return 4000
        elif grades >= 60:
            return 2000
        elif grades >= 40:
            return 1000
    elif extra_act >= 60:
        if grades >= 80:
            return 3000
        elif grades >= 60:
            return 1000
        elif grades >= 40:
            return 0
    elif extra_act >= 40:
        if grades >= 80:
            return 2000
        elif grades >= 60:
            return 0
        elif grades >= 40:
            return 0

def low_financial(extra_act, grades):
    if extra_act >= 80:
        if grades >= 80:
            return 3000
        elif grades >= 60:
            return 1000
        elif grades >= 40:
            return 0
    elif extra_act >= 60:
        if grades >= 80:
            return 2000
        elif grades >= 60:
            return 0
        elif grades >= 40:
            return 0
    elif extra_act >= 40:
        if grades >= 80:
            return 1000
        elif grades >= 60:
            return 0
        elif grades >= 40:
            return 0

financial_cond = float(input('Enter financial condition in 1 to 100: '))
extra_act = float(input('Enter the extracurricular participation in 1 to 100: '))
grades = float(input('Enter the grades of the student: '))

print(scholarship(financial_cond, extra_act, grades))
登入後複製

拒絕原因:

  1. 代碼冗餘:確定獎學金的邏輯在不同的財務需求類別(high_financial、medium_financial、low_financial)中重複,導致代碼冗餘。這可以簡化以避免重複並降低不一致的風險。
  2. 函數呼叫的複雜性:巢狀的函數呼叫和多層條件使程式碼更難以遵循和維護。分離成不同的函數有利於模組化,但由於嵌套條件的數量可能會過多且令人困惑。
  3. 獎學金金額不一致:不同經濟需求水平之間的獎學金金額決定邏輯並不總是一致,這可能會導致人們對為什麼要根據特定條件給予某些獎學金金額產生潛在的困惑。

3

grades = float(input("Enter the applicant's GPA : "))
extracurriculars = int(input("Enter the number of extracurricular activities: "))
financial_need = input("Is there a financial need? (yes/no): ").strip().lower()

eligible = True

grade_thresholds = [3.5, 3.0, 2.5] 
scholarship_level = None

for threshold in grade_thresholds:
    if grades >= threshold:
        if threshold == 3.5:
            scholarship_level = "Full Scholarship"
        elif threshold == 3.0:
            scholarship_level = "Partial Scholarship"
        elif threshold == 2.5:
            scholarship_level = "Basic Scholarship"
        break
else:
    eligible = False
    print("Applicant does not meet the minimum grade requirement.")

if eligible:
    extracurricular_threshold = 2  # Minimum number of activities required
    if extracurriculars < extracurricular_threshold:
        eligible = False
        print("Applicant does not meet the extracurricular activities requirement.")

if eligible:
    while financial_need not in ['yes', 'no']:
        financial_need = input("Please enter 'yes' or 'no' for financial need: ").strip().lower()

    if financial_need == 'no':
        scholarship_level = "Merit-based Scholarship"  # Adjust the scholarship type based on no financial need

if eligible:
    print(f"The applicant is eligible for the {scholarship_level}.")
else:
    print("The applicant is not eligible for the scholarship.")
登入後複製

拒絕原因

  1. 過於簡單化:該方法過於簡單化,因為它沒有考慮一系列條件,而是使用單一的成績閾值和財務需求的二進制檢查。這可能會忽略確定資格和獎學金金額時的重要細微差別。
  2. 硬編碼值:獎學金等級是根據 GPA 和財務需求的二進制檢查進行硬編碼的,而不考慮其他因素,例如課外參與程度或更細粒度的財務需求評估。
  3. 互動處理:如果使用者不輸入“是”或“否”,處理財務需求輸入的循環可能會導致無限循環(儘管無限循環在實踐中可能是一個罕見的問題,但它是一個潛在的缺陷).

選擇解決方案的原因及其潛在缺陷

讓我們回顧一下所提出的解決方案並分析其選擇的原因以及潛在的缺陷:

建議的解決方案

def scholarship(financial_cond, extra_act, grades):
    if financial_cond >=80:
        if extra_act >=80:
            if grades >=80:
                return 5000
            elif grades >= 60:
                return 3000
            elif grades >= 40:
                return 2000
        elif extra_act >= 60:
            if grades >=80:
                return 4000
            elif grades >= 60:
                return 2000
            elif grades >= 40:
                return 1000
        elif extra_act >= 40:
            if grades >=80:
                return 3000
            elif grades >= 60:
                return 1000
            elif grades >= 40:
                return 0
    elif financial_cond >=60:
        if extra_act >=80:
            if grades >=80:
                return 4000
            elif grades >= 60:
                return 2000
            elif grades >= 40:
                return 1000
        elif extra_act >= 60:
            if grades >=80:
                return 3000
            elif grades >= 60:
                return 1000
            elif grades >= 40:
                return 0
        elif extra_act >= 40:
            if grades >=80:
                return 2000
            elif grades >= 60:
                return 0
            elif grades >= 40:
                return 0
    elif financial_cond >= 40:
        if extra_act >=80:
            if grades >=80:
                return 3000
            elif grades >= 60:
                return 1000
            elif grades >= 40:
                return 0
        elif extra_act >= 60:
            if grades >=80:
                return 2000
            elif grades >= 60:
                return 0
            elif grades >= 40:
                return 0
        elif extra_act >= 40:
            if grades >=80:
                return 1000
            elif grades >= 60:
                return 0
            elif grades >= 40:
                return 0
    else:
        return 0

financial_cond = float(input('Enter financial condition in 1 to 100 '))
extra_act = float(input('Enter the extracurricular participation in 1 to 100 '))
grades = float(input('Enter the grades of the student '))

print(scholarship(financial_cond, extra_act, grades))
登入後複製

選擇理由

  1. 結構化方法:此解決方案採用多層決策流程,有系統地評估財務狀況、課外活動和成績。這種結構化方法可以採用清晰、有組織的方法來確定獎學金資格。

  2. 詳細評估:此解決方案涵蓋了一系列具有特定閾值的條件,包括經濟需求、課外活動和學業成績,為分配獎學金金額提供了詳細的框架。

  3. 模組化:透過分離不同層級的財務需求並評估這些層級內的其他標準,該解決方案引入了更易於管理和更新的模組化結構。

  4. 清晰的決策路徑:嵌套的 if-else 語句創建了清晰的決策路徑,明確了哪些條件導致了哪些獎學金金額。

潛在缺陷

  1. 冗餘和複雜性:程式碼包含大量冗餘。例如,對成績和課外活動的檢查會重複多次。這種冗餘增加了程式碼的複雜性,並使其更難以維護和理解。

  2. 程式碼重複:每個financial_cond範圍的決策邏輯都有相似的模式,但閾值略有不同。透過整合通用邏輯並減少重複的程式碼區塊可以避免這種重複。

  3. 硬編碼值:獎學金金額和閾值是硬編碼的,這使得它不靈活。如果需要更改標準或金額,則必須手動更新代碼。使用設定檔或常數可以提高靈活性。

  4. 缺乏輸入驗證:程式碼不會驗證輸入以確保它們在預期的範圍或類型內。例如,financial_cond、extra_act 和 Grades 預計介於 1 到 100 之間,但程式碼不會強制執行或驗證這一點。

  5. 邊緣情況:此解決方案無法穩健地處理邊緣情況。例如,如果 Financial_cond、extra_act 或 Grades 正好位於邊界值上(例如,剛好 40、60 或 80),則處理有些不明確。更清晰地處理邊界情況將使決策邏輯更精確。

  6. 沒有回饋或解釋:此解決方案僅提供獎學金金額,並沒有解釋為什麼給予一定金額。新增回饋或解釋可以提高使用者的理解和透明度。

您能根據現代標準改進建議的解決方案嗎? [提示:重構冗餘、實現輸入驗證、使用常數並增強使用者回饋]

進階解決方案:https://insightshub.in/design-a-multi-tier-decision-making-system-to-define-the-eligibility-criteria-for-a-scholarship/#h-advanced-解決方案

以上是設計一個多層決策系統來決定獎學金的資格標準。的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!