简体   繁体   中英

system that applies the function to the index on loop?

I'm attempting to create a system where pre-defined variables are run through a function and placed in lists based on the value of the variable.

global i, st1, st2, st3, classa
A = []
B = []
st1 = '13'
st2 = '3'
st3 = '17'
list = [st1, st2, st3]

i = 0
while (i<2):
    i = i + 1
    a = list[i]


def listselect():
    sn = 'student name-'
    f = 'score-'
    a = ()
    if a in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
        A.append(sn+f+a)

    elif a in {'10', '11', '12', '13', '14', '15', '16', '17', '18', '19'}:
        B.append(sn+f+a)

    print(A)
    print(B)


listselect()

However it only outputs the empty lists, without the values being added.

[]
[]

How would I be able to run this program on a while loop until all variables have been sorted?

Sorry if anything is wrong with the post formatting, i'm new to the website.

Does this do what you want?

st1 = '13'
st2 = '3'
st3 = '17'
A , B = [], []
a = list ([st1, st2, st3])

def listselect (a) :
    sn = 'student name-'
    f = 'score-'
    for element in a :
        if element in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') :
            A.append (sn+f+element)
        else : B.append (sn+f+element)  

listselect (a)
print (A, '\n', B)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM