0

I'm trying to get two values or a set of strings in separate columns. The code below will randomize two letters from the list. The issue I'm having with this is that they keep returning in vertical and I would like to print them next to each other. I tried several ways and none of them work. They either print them the same way as the below output or just print every character individually even the , \n, etc.

import random

letters = {'A', 'B', 'C'}
letter = list(letters)

for _ in range(2):
    random_letter = random.choice(letter)
    print(" ╔═══╗\n", f"║ {random_letter} ║\n", "╚═══╝", end='')

The method above returns this,

╔═══╗
║ A ║
╚═══╝╔═══╗
║ C ║
╚═══╝

This other method returns it like this,

print("╔═══╗", f"║ {random_letter} ║", "╚═══╝", end='')


╔═══╗ ║ A ║ ╚═══╝╔═══╗ ║ C ║ ╚═══╝

I would like it to return like this,

╔═══╗╔═══╗
║ A ║║ C ║
╚═══╝╚═══╝

2 Answers 2

3
import random

letters = {'A', 'B', 'C'}
letter = list(letters)

def print_random_letter_boxes(num_letters = 2):
    print("".join(["╔═══╗" for _ in range(num_letters)]))
    print("".join([f"║ {random.choice(letter)} ║" for _ in range(num_letters)]))
    print("".join(["╚═══╝" for _ in range(num_letters)]))

print_random_letter_boxes()
Sign up to request clarification or add additional context in comments.

3 Comments

It is a nice way of printing it, but not what I want. It returns the items inside a ` `.
@arm65: Works for fine me. What exactly do you mean inside a ` `?
Never mind, I didn't realize it was a function and just print it.
1

You have to initialize how many boxes you want in a variable which is hmb here:

import random

letters = {'A', 'B', 'C'}
letter = list(letters)
v = False # a bool to check if i have printed upper portions of the boxes
hmb = 2 # this is the "how many boxes" variable
for _ in range(hmb):
    random_letter = random.choice(letter)
    if not v:
        print("╔═══╗"*hmb)
        v = True # it means all upper portion printed
    if v:
        print("║",random_letter,"║",end="")
    if _ == hmb-1: # the lower part of the boxes will only be printed if its the last time of the loop
        print()
        print("╚═══╝"*hmb)

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.