I'm currently working on a task where I must store scores in a text file. This is my code thus far:
def FileHandle():
score = str(Name) + ": " + str(correct)
File = open('Test.txt', 'a')
File.write(score)
File.close()
Name = input("Name: ")
correct = input("Number: ")
FileHandle()
My question is, how would I check already existed names in the text file and only add their score, rather than name and score, to the line it existed on?
This is what the file looks like:
Jonathon: 1
Micky: 5
How it would look after adding a score:
Jonathon: 1, 4
Mickey: 5
# The score added here is Jonathon's 4
Attempt:
# Accept scores
name = input("Name: ")
correct = input("Number: ")
if name in grade_book.keys and "," in grade_book.keys <= 2():
grade_book[name] += ',' + correct
else:
grade_book[name] = correct
If you are entering many scores at a time, I suggest reading the file into memory and working with things there. Doing an open/close on the file for every score update is very inefficient.
# Read the file into a dictionary
grade_book = {}
File = open('Test.txt', 'r')
for line in File:
name, scores = line.split(':')
grade_book[name] = scores.strip()
File.close()
print grade_book
# Accept scores
name = raw_input("Name: ")
while name != "":
correct = raw_input("Number: ")
if name in grade_book.keys():
grade_book[name] += ',' + correct
else:
grade_book[name] = correct
name = raw_input("Name: ")
# Write dictionary back to the file
File = open('Test.txt', 'w')
for name, scores in grade_book.items():
out_line = name + ':' + scores + "\n"
File.write(out_line)
File.close()
Unfortunately you would have to go over the file, find the correct line, edit it and write in a temp file and then move that file to original. Best first use a data structure like dict etc. to update scores and finally when done write or persist them.
def filehandle(name,correct):
temp = open('temp', 'wb')
with open('Test.txt', 'r') as f:
for line in f:
if line.startswith(name):
line = line.strip() + correct +'\n'
temp.write(line)
temp.close()
shutils.move('temp', 'data.txt')
You need to pass in the parameters while calling the functions.
Name = input("Name: ")
correct = input("Number: ")
filehandle(name, correct)
Related
I have saved a score with a name in a csv. I would like to create an average score out of people names who are the same and display this.
score2 = str(score)#Converts the integer into the string so can be saved
fsav = (fulnam + " : " + score2)#Sets the variable as Full name of the user and the score
filsav = clsnam + ".csv" #Creates the file that the scores will be saved in
file = open(filsav , "a") #Opens the file so it can be appended
file.write(fulnam + " , ") #Writes the persons name to the
file.write(score2) #Writes the persons score to the file
file.write("\n")#Moves the file onto a new line
file.close() #Closes and saves the file
Obviously, you have to read the entire file in to do it:
import csv
def compute_average(clsnam, fulnam):
""" Determine average score in class for named person.
"""
scores = []
with open(clsnam + ".csv", 'r') as file:
for name, score in csv.reader(file):
if name.strip() == fulnam:
scores.append(int(score))
return None if not scores else float(sum(scores)) / len(scores)
File = input("Please enter the name for your txt. file: ")
fileName = (File + ".txt")
WRITE = "w"
APPEND = "a"
file = []
name = " "
while name != "DONE" :
name = input("Please enter the guest name (Enter DONE if there is no more names) : ").upper()
fileName.append(name)
fileName.remove("DONE")
print("The guests list in alphabetical order, and it will save in " + fileName + " :")
file.sort()
for U in file :
print(U)
file = open(fileName, mode = WRITE)
file.write(name)
file.close()
print("file written successfully.")
I am just practicing to write the file in Python, but something bad happened.
Here are still some errors about this:
fileName.remove("DONE")
Still showing 'str' error.
filename=filename+name
Use the above code
Python strings are immutable. Therefore you can't use append() on them. Use += instead:
fileName += name
which is shorthand for
fileName = fileName + name
Note how nothing is appended to the string, instead a new one is created and then assigned to fileName.
Try this.
I thought you have some mistaken in variable name.
aFile = input("Please enter the name for your txt. file: ")
fileName = (aFile + ".txt")
WRITE = "w"
APPEND = "a"
file = []
name = " "
while name != "DONE" :
name = input("Please enter the guest name (Enter DONE if there is no more names) : ").upper()
file.append(name)
file.remove("DONE")
print("The guests list in alphabetical order, and it will save in " + fileName + " :")
file.sort()
for U in file :
print(U)
outputfile = open(fileName, mode = WRITE)
outputfile.write(name)
outputfile.close()
print("file written successfully.")
Just right out the bat, you can not append to a toople.
fileName.append(name) #how can you append or remove anything into or from this when it contains toople?
Another thing, I don't know what version of python you are using but, I never seen expression like this
file = open(fileName, mode = WRITE) #this should be something like (file=open(fileName,"w"))
Just overall check your code. Like I said you can not add or remove stuff from a toople; only in lists and dictionaries.
append is the list's method where as fileName declared in your code is treated as string. If your intention is to append the string to file, open the file in "append" mode and write to it:
with open(aFile + ".txt", "a") as f:
f.write("appended text")
Im trying to save student's scores in a text file however I have been asked to save the student's last three scores. This sounded fairly straight forward however after attempting various different things I still can't do it. I have been trying to use their names as the variable that changes i.e. if their name is entered more than once.
here is my pitiful best attempt
please help
class_number = prompt_int_big("Before your score is saved ,are you in class 1, 2 or 3? Press the matching number")
filename = (str(class_number) + "txt")
if any(name in s for s in filename):
for score in range (0,3):
with open(filename, 'a') as f:
f.write("\n" + str(name) + " also scored " + str(score) + " on difficulty level " + str(level_of_difficulty) + "\n")
with open(filename) as f:
lines = [line for line in f if line.strip()]
lines.sort()
name[-3]
else:
with open(filename, 'a') as f:
f.write("\n" + str(name) + " scored " + str(score) + " on difficulty level " + str(level_of_difficulty) + "\n")
with open(filename) as f:
lines = [line for line in f if line.strip()]
lines.sort()
filename = (str(class_number) + "txt")
class_number is already a string--because anything a user inputs is a string. So, let's say the user inputs "1", then you get:
filename = "1txt"
Then you do:
any(name in s for s in filename):
When you loop over the string "1txt":
for s in filename
you get letters back, so you are asking:
any(name in "1" ....)
...which appears to be nonsensical.
I have been asked to save the student's last three scores.
Based on your code, I have no inkling what that even remotely means. How about you post:
I have this....
I want this....
In the meantime, here's some stuff you can try. The shelve module will make your life much easier:
import shelve
fname = "scores"
name = "John"
current_scores = [75, 98]
#shelve will add a .db extension to the filename,
#but you use the name without the .db extension.
with shelve.open(fname) as db:
old_scores = db.get(name, []) #db is dictionary-like. Get previously stored scores--if any,
# or return an empty list
old_scores.extend(current_scores) #Add the current scores to the old scores.
db[name] = old_scores[-3:] #Save the last three scores in the file.
#Time passes....
nane = "John"
current_scores = [67, 80, 41]
with shelve.open(fname) as db:
old_scores = db.get(name, [])
old_scores.extend(current_scores)
old_scores.sort()
print(old_scores)
db[name] = old_scores[-3:]
with shelve.open(fname) as db:
print(db["John"])
--output:--
[41, 67, 75, 80, 98, 98]
[80, 98, 98]
I'm quite new to Python. I would like to keep a high score in Python, and only write the new players name. I would like to implement this in a larger program, but I can't quite get this logic down yet.
How do you handle an old player setting a better score than before?
How do you deal with two players with the same name having scores?
save_name = raw_input('Enter your name. ').title()
save_score = raw_input('Enter your score. ')
text_file = open("highscores.txt", "a")
text_file.write("\n" + save_name + ' has a score of ' + save_score + "\n")
text_file.close()
print ("\n")
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
I'm assuming your goal is to read in the high scores and only add those scores that were made by a new player. If so the questions you have to ask are:
How do you handle an old player setting a better score than before?
How do you deal with two players with the same name having scores?
How do you want to display your high scores?
Personally I wouldn't do this in a text file, but write it in a dictionary and pickle it.
import pickle
high_scores = {"Adam Smith": 65536, "John Doe": 10000}
with open("highscores.pkl","wb") as out:
pickle.dump(high_scores, out)
Then when you have to write a new score:
import collections
Score = collections.namedtuple("Score", ["name","score"]) # just to make things easy
new_score = Score("Joe Schmoe",12000)
with open("highscores.pkl","rb") as in_:
high_scores = pickle.load(in_)
if new_scores.name not in high_scores:
high_scores[new_scores.name] = new_scores.score
with open("highscores.pkl","wb") as out:
pickle.dump(high_scores, out)
This will also help when displaying high scores, because you can do something like:
print("{{TITLE:^{PAGE_WIDTH}}}".format(PAGE_WIDTH=80).format(TITLE="HIGH SCORES"))
print("-" * 80)
for name,score in high_scores.items():
print("{{name:>{col_width}}} | {{score:<{col_width}}}".format(col_width=(80-3)//2).format(name=name, score=score))
The formatting in this bit is a bit overkill for your use case, but if you need to display this on a smaller screen (with fewer columns per page) you'll thank me later! Replace all the 80s with a constant and put your columns-per-page in the constant, something like MAX_WIDTH maybe. Easy peasy.
First of all, text_file.readlines(save_name) will throw a TypeError because save_name is not an integer. See the readlines documentation. Second of all, this line will not do anything as you are not assigning a variable to the return value of readlines. I am not entirely sure what you are trying to achieve here. This part of your code is working fine:
save_name = raw_input('Enter your name. ').title()
save_score = raw_input('Enter your score. ')
text_file = open("highscores.txt", "a")
text_file.write("\n" + save_name + ' has a score of ' + save_score + "\n")
text_file.close()
It will correctly write to the highscores.txt file.
If you want to print the contents of the highscores file, the last part of your code is doing that just fine.
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
The part in between these two quoted block is probably obsolete and/or we need a better specification of what you are trying to achieve.
Using your code I've made some changes to check first the high score and only add a new score if the user beats the last score:
save_name = raw_input('Enter your name. ').title()
save_score = raw_input('Enter your score. ')
last_high_score = 0
#look for highscore
try:
text_file = open("highscores.txt", "r")
for line in text_file.readlines():
#you can use regular expressions here to simplify the lookup
#get the las part of the score assuming the pattern:
#"text[ has a score of] score"
line_parts = line.split(" has a score of ")
if len(line_parts) > 1:
#removing the end \n character
line_parts = line_parts[-1].split("\n")
score = line_parts[0]
#compare the last high score with the newest
if score.isdigit() and int(score) > last_high_score:
last_high_score = int(score)
except Exception, e:
#the first time trows an exception because the high score file does not exist
pass
#check if there is a new high score
if int(save_score) > last_high_score:
text_file = open("highscores.txt", "a")
text_file.write("\n"+save_name+' has a score of '+save_score+"\n")
text_file.close()
print ("\n")
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
ave_name = raw_input('Enter your name. ').title()
save_score = raw_input('Enter your score. ')
last_high_score = 0
#look for highscore
try:
text_file = open("highscores.txt", "r")
for line in text_file.readlines():
#you can use regular expressions here to simplify the lookup
#get the las part of the score assuming the pattern:
#"text[ has a score of] score"
line_parts = line.split(" has a score of ")
if len(line_parts) > 1:
#removing the end \n character
line_parts = line_parts[-1].split("\n")
score = line_parts[0]
#compare the last high score with the newest
if score.isdigit() and int(score) > last_high_score:
last_high_score = int(score)
except Exception, e:
#the first time trows an exception because the high score file
pass
#unban user14762509 moderator fags
#check if there is a new high score
if int(save_score) > last_high_score:
text_file = open("highscores.txt", "a")
text_file.write("\n"+save_name+' has a score of '+save_score+"\n")
text_file.close()
print ("\n")
text_file = open("highscores.txt", "r")
whole_thing = text_file.read()
print (whole_thing)
text_file.close()
If you want to ONLY write the user's name, and not their high score, couldn't you just change this:
text_file.write("\n" + save_name + ' has a score of ' + save_score + "\n")
To this:
text_file.write("\n" + save_name + "\n")
I'm not sure does this helps
import time
import operator
startTime=time.time()
x=input('testonly')
endTime=time.time()
userscore=int(endTime-startTime)
scorelist=[]
beathighscore=False
scorefile=open('score.txt','r')
score=scorefile.readlines()
scorefile.close
for line in score:
scorelist.append(line.strip())
if len(score)!=0:
for item in scorelist:
oldscore=((item[0])[1])
oldscore=int(oldscore)
else:
oldscore=float('inf')
if userscore<oldscore:
print("you beat the highscore")
username=input("pls enter ur name")
newscore=(username,userscore)
scorelist.append(newscore)
with open('score.txt',"w") as writescore:
for item in scorelist:
writescore.write(''.join(str(item))+'\n')
writescore.close
count = 0
answer = ""
pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}
name = ""
def program_start():
answer = input('Do you want to make some TXT with the same passwords? y\\n :')
count = int(input('How many TXT files do you want to make?'))
name = input('Enter the hot user id:')
name = name+".TXT"
password_collector() # collect password to pass_pool dictionary
create_file() #create TXT file. it has to be in capital "TXT"
#for the safe program.
def create_file():
newTXT = open(name, "w")
newTXT.write(name + "\n \n" )
for system , password in pass_pool.items():
newTXT.write(system + ":" + password )
newTXT.close()
I get:
File "C:\Python33\mypy\txt creator.py", line 16, in create_file
newTXT = open(name, "w")
FileNotFoundError: [Errno 2] No such file or directory:
From what I look on google this error mean wrong path or file not found. But I check with sys.path and saw that "C:\Python33\mypy" in my paths, and I create the file with "w" so it should work with no problems.
When I used only the create_file() function in the shell it works with no problem.
When you set the value of name in program_start, Python creates a variable name local to that function's scope, which masks the global name, so the global value remains unchanged. In create_file you use the unchanged global name, which equals to "", and opening a file with the name "" gives you an error.
The quick-and-dirty fix would be adding
global name
in the beginning of program_start. But it is much clearer to write
count = 0
answer = ""
pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}
def program_start():
answer = input('Do you want to make some TXT with the same passwords? y\\n :')
count = int(input('How many TXT files do you want to make?'))
name = input('Enter the hot user id:')
name = name+".TXT"
password_colector() # collect password to pass_pool dic
create_file(name) #create TXT file. it has to be in capital "TXT"
#for the safe pogram.
def create_file(name):
newTXT = open(name, "w")
newTXT.write(name + "\n \n" )
for system , password in pass_pool.items():
newTXT.write(system + ":" + password )
newTXT.close()