vantablack
vantablack

Reputation: 11

Variable not being assigned in Python

I have to make a program to register a student that can choose 1, 2 or no extra subjects. To account for the multiple options, I made a function that asks for a subject ID, then translates that subject ID into a string and counts 1 in the subject total. There is also a buffer variable in the case the person needs to make changes later on.

#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

The problem is that the function goes well, and it outputs correct information... when the function is running. When it gets to confirmation, nothing is shown in Subject 1 or Subject 2, and sb1B (Subject 1 Buffer) and sb2B (Subject 2 Buffer) become zero. What am I doing wrong?

 #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))

Output if any 2 unique subjects are selected:

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

Don't bash me too hard, it's been 2 weeks since I started, so any advice is helpful!

PasteBin: https://pastebin.com/H92qSKvR

Upvotes: 0

Views: 74

Answers (1)

Hayden Eastwood
Hayden Eastwood

Reputation: 966

The problem is that you are setting the sb1B variable in the SubjectSelector() function. This variable is NOT accessible outside the function, which is why it remains empty. The following fix will work. First, fix your SubjectSelector function with one extra line:

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

Then, later down, change the line:

SubjectSelector(sbNo)

To:

sb1, sb2 = SubjectSelector(sbNo)

The above will fill variables sb1 and sb2, which you can then user later on.

Upvotes: 1

Related Questions