Python Compare values stored in variables, then print the variable name


#1

I have tried to use the max code but it printed out the largest number not the variable name.

A=0
B=0
C=0
textfile = open("Preference.txt","r")
for x in textfile:
    preferline = x.strip('\n').split(",")
    if preferline[0]=='1':
        A+=1
    elif preferline[1]=='1':
        B+=1
    elif preferline[2]=='1':
        C+=1

max(A,B,C,D,E)

Is there a code to print out the variable with the highest values…


#2

why would you want to print the variable name? The variable names are only for the internal working of the program, a end user using your program has no idea what the variables are, they are interested in the value.

as we can deduce from this question:

the only ways to accomplish this are difficult and are often best avoid


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.