未在Python中赋值的变量

2024-06-28 19:51:30 发布

您现在位置: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


Tags: 函数id主题inputifsubjectprintplease
1条回答
网友
1楼 · 发布于 2024-06-28 19:51:30

问题是您正在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,您可以稍后使用它们

相关问题 更多 >