1

This is the easiest way I can explain this.

Currently, I have a list that I am displaying by printing it every time it is changed. I want to call it the list "cards" with the following assigned to it.

cards = ["A", "B", "C", "D"]

Instead of printing the whole list every time it changes, I just want a single display of it to change. How would I go about doing this?

Mark Lunetta
  • 23
  • 1
  • 7
  • 1
    Where are you printing it? Are you using a gui library? Also are you saying that say for instance the second element changes from `"B"` to `"S"`, would you want to just print "S"? – Paul Rooney Aug 24 '18 at 02:24
  • Do you want to just run the program and then want it to keep on changing on its own? – Udit Hari Vashisht Aug 24 '18 at 02:25

2 Answers2

1
>>> cards = ["A", "B", "C", "D"]    
>>> from time import sleep
>>> for i in range(400):
>>>     print("\r" + str(cards), end="")
>>>     sleep(0.5)

Taken from this stackoverflow post

Won't this serve your purpose? You are reprinting the whole thing, but in the same position, so it makes no difference unless speed is a concern

kevinkayaks
  • 2,636
  • 1
  • 14
  • 30
1

You could use a small GUI with tkinter to display your values, or, derived from @kevinkayaks suggestion, print the cards in the console:

import tkinter as tk
import random


def print_cards(cards):
    print("\r" + ' '.join(cards), end='', flush=True)


def shuffle_cards():
    random.shuffle(cards)
    print_cards(cards)
    for card, text in zip(cards, texts):
        text.set(card)
    root.after(1000, shuffle_cards)


cards = ["A", "B", "C", "D"]

root = tk.Tk()
texts = [tk.StringVar() for _ in range(4)]
for idx in range(4):
    tk.Label(root, textvariable=texts[idx]).grid(row=0, column=idx)
    texts[idx].set(cards[idx])

shuffle_cards()

root.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80