簡體   English   中英

變量未在 Python 中分配

[英]Variable not being assigned in Python

我必須制作一個程序來注冊一個可以選擇 1、2 或不選擇額外科目的學生。 為了說明多個選項,我創建了一個函數,要求提供一個主題 ID,然后將該主題 ID 轉換為一個字符串,並在主題總數中計數 1。 還有一個緩沖區變量,以防此人稍后需要進行更改。

#SubjectSelector
def SubjectSelector(noOfSb):
    sb2B = 0
    sb1B = 0
    PTotal = 0
    ChTotal = 0
    HTotal = 0
    GTotal = 0
    CSTotal = 0
    sb1 = 'bruh'
    sb2 = 'bruh'
    for i in range(noOfSb):
        i = i + 1
        print('Select Subject ' + str(i) + '.\n\n')
        print('''        1. Physics
        2. Chemistry
        3. History
        4. Geography
        5. Computer Science
        ''')
        #Input Validation
        while True:
            try:
                sbID = int(input())
            except ValueError:
                print('Invalid input, please try again.')
                continue
            if sbID < 1:
                print('Invalid input, please try again.')
                continue
            if sbID > 5:
                print('Invalid input, please try again.')
                continue
            if sbID == sb1B:
                print("Same subject chosen, please try again.")
                continue
            else:
                break
        if i == 1:
            sb1B = sbID
        else:
            sb2B = sbID
        print(str(sb1B))
        print(str(sb2B))
        #Subject Matcher
        if sbID == 1:
            PTotal = PTotal + 1
            sbID = "Physics"
        if sbID == 2:
            ChTotal = ChTotal + 1
            sbID =  "Chemistry"
        if sbID == 3:
            HTotal = HTotal + 1
            sbID = "History"
        if sbID == 4:
            GTotal = GTotal + 1
            sbID = "Geography"
        if sbID == 5:
            CSTotal = CSTotal + 1
            sbID = 'Computer Science'
        print(sbID)
        if i == 1:
            sb1 = sbID
            sb2 = ''
        if i == 2:
            sb2 = sbID

問題是函數運行良好,並且在函數運行時輸出正確的信息...... 當它得到確認時,主題 1 或主題 2 中沒有顯示任何內容,並且 sb1B(主題 1 緩沖區)和 sb2B(主題 2 緩沖區)變為零。 我究竟做錯了什么?

 #Information Confirmation
    print("Confirm the information below.\n\n")
    print('Name: ' + str(stName) + "")
    print('ID Number: ' + str(stID) + '')
    print('Number of Subjects: ' + str(sbNo) + '')
    print('Subject 1: ' + str(sb1))
    print('Subject 2: ' + str(sb2) + '\n')
    print('''    1. Edit Name
   2. Edit Subjects
   3. Cancel
   4. Confirm''')
   #Debugging Info
    print(sb1)
    print(sb2)
    print(str(sb1B))
    print(str(sb2B))

如果選擇了任何 2 個唯一主題,則輸出:

Confirm the information below.

Name: yeet
ID Number: 1001
Number of Subjects: 2
Subject 1: 
Subject 2: 

    1. Edit Name
    2. Edit Subjects
    3. Cancel
    4. Confirm


0
0

不要太猛烈地抨擊我,自從我開始已經兩周了,所以任何建議都是有幫助的!

PasteBin: https ://pastebin.com/H92qSKvR

問題是您在 SubjectSelector() 函數中設置了 sb1B 變量。 該變量在函數外部不可訪問,這就是它保持為空的原因。 以下修復將起作用。 首先,用一個額外的行修復你的 SubjectSelector 函數:

def SubjectSelector(noOfSb):
    sb2B = 0
    sb1B = 0
    PTotal = 0
    ChTotal = 0
    HTotal = 0
    GTotal = 0
    CSTotal = 0
    sb1 = 'bruh'
    sb2 = 'bruh'
    for i in range(noOfSb):
        i = i + 1
        print('Select Subject ' + str(i) + '.\n\n')
        print('''        1. Physics
       2. Chemistry
       3. History
       4. Geography
       5. Computer Science
       ''')
        #Input Validation
        while True:
            try:
                sbID = int(input())
            except ValueError:
                print('Invalid input, please try again.')
                continue
            if sbID < 1:
                print('Invalid input, please try again.')
                continue
            if sbID > 5:
                print('Invalid input, please try again.')
                continue
            if sbID == sb1B:
                print("Same subject chosen, please try again.")
                continue
            else:
                break
        if i == 1:
            sb1B = sbID
        else:
            sb2B = sbID
        print(str(sb1B))
        print(str(sb2B))
        #Subject Matcher
        if sbID == 1:
            PTotal = PTotal + 1
            sbID = "Physics"
        if sbID == 2:
            ChTotal = ChTotal + 1
            sbID =  "Chemistry"
        if sbID == 3:
            HTotal = HTotal + 1
            sbID = "History"
        if sbID == 4:
            GTotal = GTotal + 1
            sbID = "Geography"
        if sbID == 5:
            CSTotal = CSTotal + 1
            sbID = 'Computer Science'
        print(sbID)
        print ('i is ' + str(i))
        if i == 1:
            print ('sbID is ' + str(sbID))
            sb1 = sbID
            sb2 = ''
        if i == 2:
            sb2 = sbID
    return (sb1, sb2) # this is a new line to return the variables sb1 and sb2

然后,稍后,更改行:

SubjectSelector(sbNo)

到:

sb1, sb2 = SubjectSelector(sbNo)

以上將填充變量 sb1 和 sb2,您可以稍后使用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM