#๐Ÿ”’ dice roller program-how do i print horizontal instead of vertical

81 messages ยท Page 1 of 1 (latest)

plucky wolf
#

program:

import random

dice_art = {
    1: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚         โ”‚",
        "โ”‚    โ—    โ”‚",
        "โ”‚         โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜"),
    2: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚  โ—      โ”‚",
        "โ”‚         โ”‚",
        "โ”‚      โ—  โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜"),
    3: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚  โ—      โ”‚",
        "โ”‚    โ—    โ”‚",
        "โ”‚      โ—  โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜"),
    4: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚  โ—   โ—  โ”‚",
        "โ”‚         โ”‚",
        "โ”‚  โ—   โ—  โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜"),
    5: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚  โ—   โ—  โ”‚",
        "โ”‚    โ—    โ”‚",
        "โ”‚  โ—   โ—  โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜"),
    6: ("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
        "โ”‚  โ—   โ—  โ”‚",
        "โ”‚  โ—   โ—  โ”‚",
        "โ”‚  โ—   โ—  โ”‚",
        "โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜")
}

dice = []
total = 0
low = 1
high = 6
num_of_dice = int(input("How many dice?: "))

for die in range(num_of_dice):
    dice.append(random.randint(low,high))

print(dice)
for die in range(num_of_dice):
    for line in dice_art.get(dice[die]):
        print(line)

for die in dice:
    total += die
print(f"Your total is {total}")
fathom sedgeBOT
#

@plucky wolf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plucky wolf
#

that outputs:

#
[1, 2, 4]
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚         โ”‚
โ”‚    โ—    โ”‚
โ”‚         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  โ—      โ”‚
โ”‚         โ”‚
โ”‚      โ—  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  โ—   โ—  โ”‚
โ”‚         โ”‚
โ”‚  โ—   โ—  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Your total is 7
#

how do i print the dices horizontally next to each other rather than vertical?

thorny prism
#

change the end argument of print

#

it's a newline by default

#

!d print

fathom sedgeBOT
#

print(*objects, sep=' ', end='\n', file=None, flush=False)```
Print *objects* to the text stream *file*, separated by *sep* and followed by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like [`str()`](https://docs.python.org/3/library/stdtypes.html#str) does and written to the stream, separated by *sep* and followed by *end*. Both *sep* and *end* must be strings; they can also be `None`, which means to use the default values. If no *objects* are given, [`print()`](https://docs.python.org/3/library/functions.html#print) will just write *end*.

The *file* argument must be an object with a `write(string)` method; if it is not present or `None`, [`sys.stdout`](https://docs.python.org/3/library/sys.html#sys.stdout) will be used. Since printed arguments are converted to text strings, [`print()`](https://docs.python.org/3/library/functions.html#print) cannot be used with binary mode file objects. For these, use `file.write(...)` instead.
plucky wolf
#

which print statement?

thorny prism
#

the one where you print the dice

#

you might need to add an extra newline to the last print as well, so it's not on the same line as the dice

plucky wolf
#

so do i change it to end=" " ?

thorny prism
#

oh wait nvm

#

these are multiline arts aren't they

plucky wolf
#

yep

thorny prism
#

hmm gimme a sec

#

think you can zip them

plucky wolf
plucky wolf
#

although

#

the guy in the video printed it out like this:

thorny prism
#

is this an assignment or something where you aren't allowed to use it?

plucky wolf
#
for line in range(5):
    for die in dice:
        print(dice_art.get(die)[line], end="")
    print()

plucky wolf
thorny prism
#

tldr what you need to do is collect all of the dice art you want to print in one place, then you need to print the first line of each dice, then the second and so on

#

zip would easily pair each of those lines together for you, but it's not required

plucky wolf
plucky wolf
thorny prism
#

the printing horizontally part you sent is your own code? a template from the exercise? the guy's code who is demonstrating the exercise?

plucky wolf
#

the vertical version i sent in the beginning was my code yes, he suggested how to do it horizontally as well so that part is his code, i dont understand what he did

thorny prism
#

okay, I made a little prototype that works without zip so I should be able to help now

#

so obviously you will want a nested for loop

#

have you played around with 2d lists yet?

plucky wolf
#

yess

thorny prism
#

okay so imagine you have a 2d list here, a list of your lists (tuples in this case) that contain the dice art

#

instead of starting in the top left, where the top of the first dice is, then going down to the next part of that dice you want to start at the top left, then move to the right where the top of the next dice is

#

so your two axes here are the number of lines a dice art (5) and the number of dice you want to print

#

for a little pseudocode

for line_index in number_of_dice_art_lines:
  for dice_index in number_of_dice_i_want_to_print:
    #pick the dice from your 2d list and select the line you want, print that
  #print a new line to prepare for the next set of dice
plucky wolf
#

so we are going to create a neww variable? for line index

#

my brain isnt braining ahaha

thorny prism
plucky wolf
#

ohh ok

thorny prism
#

wait I'm confused

thorny prism
#

you're just asking how it works?

#

bro lmfao I thought it was broken and needed to be fixed, it ended up at the same solution

plucky wolf
#

ahahah nooo

#

it workss yes

#

i just dont understand it xd

thorny prism
#

gotchu gotchu

#
# PRINT HORIZONTALLY
for line in range(5):
    for die in dice:
        print(dice_art.get(die)[line], end="")
    print()
#

so you understand that die is the numbers that were chosen randomly right?

#

and that line is the index of the line in the dice art you want to print?

plucky wolf
#

yepp

#

also sorry if i reply late in intervals im doing smthh atm

thorny prism
#

s'all good, but what is your question then?

#

are you asking why the line part is the first for loop and the die part is the second?

plucky wolf
#

dice_art.get(die) gets the value of the key number (e.g. 2: corresponding art)
why did we add [line] after it?

#

and how are they related/connected?

thorny prism
#

for example let's say die = 1 and line = 0

#
dice_art.get(die) 

#returns

("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”",
"โ”‚         โ”‚",
"โ”‚    โ—    โ”‚",
"โ”‚         โ”‚",
"โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜")
#

this tuple has 5 elements, which are at indices 0-4

#

if I do dice_art.get(die)[line] I will be indexing the first line of my dice

#

so

dice_art.get(die)[line]

#returns

"โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”"
#

since the die part is the inner loop you will go to the next dice in your list, and print the same line

plucky wolf
#

so if i, in another program, do smth like 1[line]

#

ill get the first index or occurence of smth

#

ok that question doesnt make sense xd

#

but i get ittt now

#

and then end to put a space

#

so it kinda prints row by row next to each other

thorny prism
#

if you have a box box = ['a','b','c'] and you want the first index you'd do box[0]

#

right

plucky wolf
#

thank you so much, sorry for the headache i've caused xd

thorny prism
#

lol, np

plucky wolf
#

!close

fathom sedgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.