Reputation: 166
I have a string that is constantly getting longer for each iteration (moves in a game). I want to print this string to my window and the window space is 75 characters in a row, then I need to switch line.
Condition: The list contains the move numbers and corresponding moves for each side:
my_str = '1.move movee 2.mov moveee 3.mo mover'
The split can't be in the middle of a move so it needs to happen between the moves:
my_str1 = '1.move movee 2.mov moveee'
my_str2 = '3.mo mover'
Is there a nice way of doing this without lots and lots of loops and if statements?
Upvotes: 0
Views: 63
Reputation: 23119
Here's something that works by first extracting each move (might be valuable to you in itself) and then prints the list of moves with line length logic:
import re
my_str = '1.move movee 2.mov moveee 3.mo mover 4.move movee 5.mov moveee 6.mo mover 7.move movee 8.mov moveee 9.mo mover 10.move movee 11.mov moveee 12.mo mover 13.move movee 14.mov moveee 15.mo mover'
moves = re.findall(r"\S+\s+\S+", my_str)
line = ''
for move in moves:
if len(line) + len(move) + 1 > 75:
print(line)
line = move
elif line:
line += ' ' + move
else:
line = move
if (line):
print(line)
Result:
1.move movee 2.mov moveee 3.mo mover 4.move movee 5.mov moveee 6.mo mover
7.move movee 8.mov moveee 9.mo mover 10.move movee 11.mov moveee
12.mo mover 13.move movee 14.mov moveee 15.mo mover
Upvotes: 1
Reputation: 364
Try storing your moves in a list.
Adding a move:
moves.append("move movee")
Reading moves:
moves[0] # First move
moves[1] # Second move
moves[-1] # Last move
moves[-2] # Second to last move
Iterating through moves:
for move in moves:
print(move)
Here is a function that will print your moves:
moves = ["nassssssssssss ssssss sssssssssssssss", "nasdasdasda sdasdas", "eas dasdasdasd", "easdasdasda sdasdsad", "sasda dsasdasd"]
def formatMoves(moves, screenwidth=75):
out = ""
for i in range(len(moves)):
move = moves[i]
if len(out) == 0 or len(out.split("\n")[-1]) == 0:
# Start a new line
out += f"{str(i+1)}.{move}"
elif len(out.split("\n")[-1]) + len(str(i+1)) + 2 + len(move) <= screenwidth:
# Continue a line
out += f" {str(i+1)}.{move}"
else:
# Start a new line
out += f"\n{str(i+1)}.{move}"
return out
print("-"*75)
print(formatMoves(moves))
Output (the first line just shows it is 75 chars long):
---------------------------------------------------------------------------
1.nassssssssssss ssssss sssssssssssssss 2.nasdasdasda sdasdas
3.eas dasdasdasd 4.easdasdasda sdasdsad 5.sasda dsasdasd
Upvotes: 0
Reputation: 27577
Here is how you can use the end
parameter of the print
function:
my_str = '1.move movee 2.mov moveee 3.mo mover'
count = 0
length = len(my_str)
for i, v in enumerate(my_str):
if i - 1 < length :
if count >= 75 and my_str[i + 1].isdigit():
count = 0
s = '\n'
else:
count += 1
s = ''
print(v, end=s)
With end=''
as a print
parameter, when you proceed to print again, the output will be printed on the same line.
if count >= 75 and i == ' ':
tells python when to switch a line; when the number of characters reach 75
, and the current character needs to be a ' '
so to not cut a move.
Upvotes: 0