The Student Room Group

Need help on Python Programming task please

Hiya
I'm doing a gcse computer science coursework to program a game.
I want to append data to a text file but not as a string but as saved data such as player name and their high score, and then the file should store the data
Here's a snippet of my rough program:
d = input("please enter player name:")
score = 12
testfile = open("needu.txt","r")
record1 = testfile.read(100)
print(record1)
testfile = open("needu.txt","a")
record = d,"1",score
print(record)
testfile.close()

New data is printed out but isn't stored in the text file, also it is printed in brackets, I do not want that, can anyone help please?
Original post by geniequeen48
I want to append data to a text file but not as a string but as saved data such as player name and their high score, and then the file should store the data

I think you're a little confused about strings and data. Strings are just letters and numbers that can make words. Data is the information inside your program that it uses. A string is a data type, so when you want to save the player name and the high score (your data) you'd save it as a string.
Original post by geniequeen48
New data is printed out but isn't stored in the text file

This is because you're using print(). All this does is write the string to your terminal window.
You've opened the file so you need to use testfile.write(record) to store that data in the text file.
Original post by geniequeen48
also it is printed in brackets

This can be a little confusing because there are two versions of Python - Python2 and Python3. They're very similar but there are a few fundamental differences, one of with is how you print to the terminal.
In Python2 there is a print statement which does not use brackets and is used like so: print "Hello world" whereas in Python3 it is a print function which is used like this: print("Hello world":wink: so it looks like you're using Python2. You can either change your print functions to use print statements, or if you are using Python 2.6 or later you can import the print function using from __future__ import print_function.


Also, just to make you aware, the testfile.read(100) is reading the first 100 bytes of the file, is this what you intend? If you have a file with more than 100 bytes you're going to miss it from your print. It would be much better to print the file line by line. You should also ensure you are closing the file once you've finished with it in read mode before you open it in append mode.
(edited 8 years ago)
Haha well I don't want to add string to a text file. Basically I used the variable called "d" so d = input("Please enter playername:")
Then after my mini quiz I have the total of points in the variable called "score". I then want to add the variables d and score to the leader board along with the piece of string "Level 1" all on the same line. Is it possible to do that? So when I open the leader board I should see a new player name, their score and their level.
Original post by geniequeen48
Haha well I don't want to add string to a text file. Basically I used the variable called "d" so d = input("Please enter playername:":wink:
Then after my mini quiz I have the total of points in the variable called "score". I then want to add the variables d and score to the leader board along with the piece of string "Level 1" all on the same line. Is it possible to do that? So when I open the leader board I should see a new player name, their score and their level.

file.write("Level 1 {0}:{1}".format(d, score))

Quick Reply

Latest

Trending

Trending