-1

I have a small command-line hold'em hand generator:

hole_cards = deck.draw(2)
h1, h2 = hole_cards
print(f'Your Hole Cards: {h1} | {h2}\n')

flop_cards = deck.draw(3)
f1, f2, f3 = flop_cards
print(f'Flop: {f1} | {f2} | {f3}\n')

turn_card = deck.draw(1) 
t = turn_card[0]
print(f'Turn: {f1} | {f2} | {f3} | {t}\n')

river_card = deck.draw(1)
r = river_card[0]
print(f'River: {f1} | {f2} | {f3} | {t} | {r}\n')

Which outputs like this:

Your Hole Cards: ♦Four♦ | ♣Five♣

Flop: ♣Two♣ | ♣Ace♣ | ♦Two♦

Turn: ♣Two♣ | ♣Ace♣ | ♦Two♦ | ♠Seven♠

River: ♣Two♣ | ♣Ace♣ | ♦Two♦ | ♠Seven♠ | ♠Ace♠

Is there any way I could, instead of printing the turn and river after the flop, replace the word flop with turn and then river? I know that I can print the new cards on the same line, but I don't know how to replace the already-printed word "flop" or "turn"

Alec
  • 8,529
  • 8
  • 37
  • 63
  • You can go back to the start of the line by printing ```'\r'```. You can't replace content from previous lines unless you clear the terminal and print everything again; or use a library like curses. – khelwood May 13 '19 at 20:41
  • https://stackoverflow.com/questions/465348/how-can-i-print-over-the-current-line-in-a-command-line-application/465360#465360 – John May 13 '19 at 20:41

2 Answers2

0

Try something like this:

import time

for i in range(5):
    print('{} of 5'.format(i), end='\r')
    time.sleep(1)
0

add end="\r" to youre print statements and youre output will replace with new content:

hole_cards = deck.draw(2)
h1, h2 = hole_cards
print(f'Your Hole Cards: {h1} | {h2}', end="\r")

flop_cards = deck.draw(3)
f1, f2, f3 = flop_cards
print(f'Flop: {f1} | {f2} | {f3}', end="\r")

turn_card = deck.draw(1) 
t = turn_card[0]
print(f'Turn: {f1} | {f2} | {f3} | {t}', end="\r")

river_card = deck.draw(1)
r = river_card[0]
print(f'River: {f1} | {f2} | {f3} | {t} | {r}', end="\r")
mh. bitarafan
  • 886
  • 9
  • 16