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?

2
  • 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"? Commented Aug 24, 2018 at 2:24
  • Do you want to just run the program and then want it to keep on changing on its own? Commented Aug 24, 2018 at 2:25

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.