0

I'm trying to make a script that tells the user where they left off in the game (Ex: The Attic, The Basement, ect.). The problem is that I'm trying to get all of it to print on the same line, but the period at the end of the sentence prints on a different line.

Here is the code

f = open(cd+"/Data/currentGame", "r")
l = open(cd+"/Data/currentLocation", "r")

current_game = f.read()
current_location_lines = l.readlines()
current_location = current_location_lines[0]

f.close()
l.close()

print("You have an existing game where you left off in "+str(current_location)+".")

The problem is that it outputs this:

You have an existing game where you left off in the attic
.

(The period is on a different line.)

1 Answers1

0

It's is because the line you read is always followed by a \n character. You need to remove it. Hence, just replace that last part of your code with this. .strip() will do for you. str(current_location).strip("\n")

Whenever you read a line from a text file, it adds a \n character to tell that new line started from there. You need to remove that while printing or it will mess up with your current statement

Your correct statement is

print("You have an existing game where you left off in "+str(current_location).strip("\n")+".")