【问题标题】:Replace output in Python [duplicate]替换Python中的输出[重复]
【发布时间】:2019-09-30 20:12:33
【问题描述】:

我有一个小型的命令行德州扑克牌生成器:

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')

这样的输出:

Your Hole Cards: ♦Four♦ | ♣Five♣

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

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

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

有什么办法可以代替翻牌后的转牌和河牌,而是用转牌和河牌代替翻牌?我知道我可以在同一行打印新牌,但我不知道如何替换已经打印的单词“flop”或“turn”

【问题讨论】:

  • 您可以通过打印'\r' 回到行首。除非您清除终端并再次打印所有内容,否则您无法替换前几行的内容;或者使用curses之类的库。

标签: python printing


【解决方案1】:

试试这样的:

import time

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

【讨论】:

    【解决方案2】:

    将 end="\r" 添加到您的打印语句中,您的输出将替换为新内容:

    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")
    

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2013-10-19
      • 1970-01-01
      • 2019-08-11
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2020-05-20
      相关资源
      最近更新 更多