0
name = input("What is your name")
myFile = open(name, 'score.txt', 'wt')
myFile.write('score: 6')
myFile.close()

As you can see this program creates a ".txt" file where it is saved, what I want to know is if I can name the file a name, for example Sam inputs his name I want the file to save itself as "Sam score.txt" with the score 6 inside it, is this possible. Thanks. - P.S kinda new so don't really know if this is correct thanks.

2 Answers 2

1

Simply add the + operator to concatenate the variable and string.

name = raw_input("What is your name?")
myFile = open(name + ' score.txt', 'w')
myFile.write('Score: 6')
myFile.close()

You'll notice I've added a space in ' score.txt' to ensure it becomes 'Sam score' rather than 'Samscore'.

Sign up to request clarification or add additional context in comments.

Comments

0

Do this:-

name = raw_input("What is your name")
myFile = open(name+'score.txt', 'wt') #Concatenate the string.
myFile.write('score: 6')
myFile.close()

Or use with statement

name = raw_input("What is your name")
with open(name+'score.txt', 'wt') as f:
    f.write('score: 6')

You don't need to specifically close it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.