Advertisement
brilliant_moves

ScoreUpdater.py

Mar 26th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. '''
  2. The user enters THIER name and it's saved. If ,at any point, they do the quiz again with the same name or will just add to the line that THIER name was saved on orginally. So I'm asking for two things:
  3. 1) it checks if the users name is in the text file
  4. 2) it adds information to that line (where the users name was)
  5. First, create a text file "Scores.txt" with each line structured like this:
  6.  
  7. Name: Humzah Score: 10
  8. '''
  9.  
  10. def findAndUpdate(name, new_score, scores):
  11.     all_text = ""
  12.     found = False
  13.     with open(scores) as f:
  14.         for line in f:
  15.             if len(line)>0:
  16.                 words = line.rstrip().split(" ")
  17.                 if words[1] == name:
  18.                     found = True
  19.                     words[3] = str(new_score)
  20.                 all_text += words[0]+" "+words[1]+" "+words[2]+" "+words[3]+"\n"
  21.  
  22.     if not found:
  23.         print ("Name: "+name+" was not found.")
  24.     else:
  25.         fw = open(scores, "w")
  26.         fw.write(all_text)
  27.         print(all_text)
  28.         print("Saving new data...")
  29.  
  30.         #close opened file
  31.         fw.close()
  32.  
  33. def main():
  34.     findAndUpdate("Yahooligan", 5, "Scores.txt")
  35.     findAndUpdate("Humzah", 20, "Scores.txt")
  36.  
  37. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement