Friday 15 July 2011

python - Trouble printing the highest value read from a file -


So I made a quiz. I've saved user quiz in a text file from Python. I have come to know how to allow a teacher to see the user names in alphabetical order with their score from the text file.

  def alpha (): b = open ("class x.txt", "r") fo = sorted for x in sorted (fo) .Redline (): Print (X) DEF beta (): B = Open ("Class Y.txt", "R") for fo = b.readlines () Sorted in (fo): Impression (X) Def charlie (): b = open (sorted "class z.txt", "r") for fo = b.readlines () sorted (fo): print (x) option = input ("what You want to score a score by / from the score: a) Alphabetical with the highest score If the option == "A": if class == "X": alpha () Eleph Class == "Y": Beta () Eleph class == "Z": Charlie () ` 

Whatever I am trying to do, it displays the names of all the characters from the text file, with which I have been successful.

My text file looks like this.

 

  Joe: 2 Prison: 4 Lawrence: 1 Harry: 2 Nakita: 2  

I was wondering if anyone Help me to explain how I Received the highest with the user name score in order to have the textfile like to include it as shown above I have used version 3.2.2 Python.

imported from the operator
  operator lines = [line.strip (). Split (':') for each line in open ('results.txt')] in each row: each [1] = int (each [1]) lines.sort (key = itemgetter (1)) in each row : Print [0], ':', each [1]  

Output:

  Lawrence: 1 Jo: 2 Harry: 2Nakita: 2 Gel: 4  

This prints the list of names sorted by the order of ascending sequence. If you want this to be done in alphabetical order (key = itemgetter (0)) Use


No comments:

Post a Comment