Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- 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:
- 1) it checks if the users name is in the text file
- 2) it adds information to that line (where the users name was)
- First, create a text file "Scores.txt" with each line structured like this:
- Name: Humzah Score: 10
- '''
- def findAndUpdate(name, new_score, scores):
- all_text = ""
- found = False
- with open(scores) as f:
- for line in f:
- if len(line)>0:
- words = line.rstrip().split(" ")
- if words[1] == name:
- found = True
- words[3] = str(new_score)
- all_text += words[0]+" "+words[1]+" "+words[2]+" "+words[3]+"\n"
- if not found:
- print ("Name: "+name+" was not found.")
- else:
- fw = open(scores, "w")
- fw.write(all_text)
- print(all_text)
- print("Saving new data...")
- #close opened file
- fw.close()
- def main():
- findAndUpdate("Yahooligan", 5, "Scores.txt")
- findAndUpdate("Humzah", 20, "Scores.txt")
- main()
Advertisement
Add Comment
Please, Sign In to add comment