Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    1
    ReputationRep:
    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...
    Offline

    6
    ReputationRep:
    Hi there,

    Where are the variables D and E coming from?
    And can you give an example of what the input file looks like?
    Offline

    12
    ReputationRep:
    you could try

    biggest = max(A,B,C,D,E)
    if A == biggest:
    print ("A")
    if B == biggest:
    print("B")

    etc for each variable. This will find the biggest value and print the names of all the variables with that value. If you only want it to print the name of one of the variables with the maximum value, use elif instead of if.
    Posted on the TSR App. Download from Apple or Google Play
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Did TEF Bronze Award affect your UCAS choices?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.