Turn on thread page Beta
    • Thread Starter
    Offline

    4
    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

    8
    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
    • Thread Starter
    Offline

    4
    ReputationRep:
    (Original post by Lelanor)
    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.
    thx alot
 
 
 

University open days

  1. University of Bradford
    University-wide Postgraduate
    Wed, 25 Jul '18
  2. University of Buckingham
    Psychology Taster Tutorial Undergraduate
    Wed, 25 Jul '18
  3. Bournemouth University
    Clearing Campus Visit Undergraduate
    Wed, 1 Aug '18
Poll
How are you feeling in the run-up to Results Day 2018?

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

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