Reputation: 61
Basically, my assignment is to simulate rolling dice and printing them out side by side depending on what was rolled. The dice have take up 5 lines each so I need a way to print out all 5 lines next to another die result of all 5 lines.
The program that I have been trying attempted to convert the functions into regular strings but it wasn't working and I also tried putting ,end = '' in the functions themselves and simply put them right under each other but I had no luck there either. I only posted in the case of if the first die is a 1.
import random
def roll_dice():
return random.choice(['1','2','3','4','5','6'])
def display_die1():
print("+-------+")
print("| |")
print("| * |")
print("| |")
print("+-------+")
def display_die2():
print("+-------+")
print("| * |")
print("| |")
print("| * |")
print("+-------+")
def display_die3():
print("+-------+")
print("| * |")
print("| * |")
print("| * |")
print("+-------+")
def display_die4():
print("+-------+")
print("| * * |")
print("| |")
print("| * * |")
print("+-------+")
def display_die5():
print("+-------+")
print("| * * |")
print("| * |")
print("| * * |")
print("+-------+")
def display_die6():
print("+-------+")
print("| * * * |")
print("| |")
print("| * * * |")
print("+-------+")
def main():
pic1, pic2, pic3, pic4, pic5, pic6 = display_die1(), display_die2(), display_die3(), display_die4(), display_die5(), display_die6()
print(f"Craps: A Popular Dice Game")
prompt = input(f"Press <Enter> to roll the dice.")
if prompt == (""):
x = roll_dice()
y = roll_dice()
add = int(x) + int(y)
if x == '1':
if y == '1':
print(pic1, end = '')
print(pic1)
if y == '2':
print(pic1, end = '')
print(pic2)
if y == '3':
print(pic1, end = '')
print(pic3)
if y == '4':
print(pic1, end = '')
print(pic4)
if y == '5':
print(pic1, end = '')
print(pic5)
if y == '6':
print(pic1, end = '')
print(pic6)
stopper = int(x) + int(y)
if add == (7 or 11):
print(f"You rolled a {add} on your first roll.")
print(f"You win!")
elif add == (2 or 3 or 12):
print(f"You rolled a {add} on your first roll.")
print(f"You lose!")
else:
print(f"You rolled a {add} on your first roll.")
print(f"\nThat's your point. Roll it again before you roll a 7 and lose!")
while add != (7 or stopper):
proceed = input(f"\nPress <Enter> to roll the dice.")
if proceed == (""):
x = roll_dice()
y = roll_dice()
add = int(x) + int(y)
if (add == 7):
print(f"You rolled a 7.")
print(f"You lose!")
break
elif (add == stopper):
print(f"You rolled a {stopper}.")
print(f"You win!")
break
else:
print(f"You rolled a {add}")
main()
I would expect the output to be two dice displaying next to each other with three spaces in between but the actual output is a very jumbled, twisted version of the two dice on one line.
Upvotes: 2
Views: 1082
Reputation: 318
Edit:
I know that there is already an answer selected to this post but here is an alternate solution using the yield keyword which can be used in sequences. I had this thought before but didn't have enough time to make the code for it. If anything, it is nice to see another way to accomplish the same thing. :)
def display_die1():
yield ("+-------+")
yield ("| |")
yield ("| * |")
yield ("| |")
yield ("+-------+")
def display_die2():
yield ("+-------+")
yield ("| * |")
yield ("| |")
yield ("| * |")
yield ("+-------+")
dice1 = display_die1()
dice2 = display_die2()
for line in range(0,5):
print(f"{next(dice1)} {next(dice2)}")
Here is the output:
+-------+ +-------+
| | | * |
| * | | |
| | | * |
+-------+ +-------+
I hope it helps!
################ My Older answerUsing docstrings instead of printing each individual line will give you a more consistent output.
def display_die1():
dice = '''
+-------+
| |
| * |
| |
+-------+
'''
return dice
def display_die2():
dice = '''
+-------+
| * |
| |
| * |
+-------+
'''
return dice
print(f"{str(display_die1())} {str(display_die2())}")
Not side by side but one on top of the other in this case.
Here is the output
python ./testing/dice_test.py
+-------+
| |
| * |
| |
+-------+
+-------+
| * |
| |
| * |
+-------+
Upvotes: 0
Reputation: 1236
One big problem is in this line:
pic1, pic2, pic3, pic4, pic5, pic6 = display_die1(), display_die2(), display_die3(), display_die4(), display_die5(), display_die6()
Let's simplify it to a single assignment and find the mistake:
pic1 = display_die()
will result in the following:
In the console:
+-------+
| * * * |
| |
| * * * |
+-------+
And pic1 == None
.
Another problem is, that you can only print line by line and not jump back. So you need a mechanism, to print in one line two "lines" for each dice.
A solution would be to store every line of a dice in a list element:
def get_dice_1():
lines = [
"+-------+",
"| * * * |",
"| |",
"| * * * |",
"+-------+"
]
To print two dice:
def print_dices(list_dice_1, list_dice_2):
for i range(len(list_dice_1)):
line = list_dice_1[i] + " " + list_dice_2[i]
print(line)
Upvotes: 2
Reputation: 142661
You have to keep dice as lists of rows
die3 = [
"+-------+",
"| * |",
"| * |",
"| * |",
"+-------+",
]
die4 = [
"+-------+",
"| * * |",
"| |",
"| * * |",
"+-------+",
]
and then you can use zip()
to create pairs [first row of die3, first row of die4]
, [second row of die3, second row of die4]
, etc. And then you can concatenate pair using "".join()
and display it
for rows in zip(die3, die4):
#print(rows)
print(''.join(rows))
+-------++-------+
| * || * * |
| * || |
| * || * * |
+-------++-------+
you can do the same with more dice
for rows in zip(die3, die4, die4, die3):
print(''.join(rows))
+-------++-------++-------++-------+
| * || * * || * * || * |
| * || || || * |
| * || * * || * * || * |
+-------++-------++-------++-------+
If you need space between dice then use space in " ".join(rows)
Upvotes: 3
Reputation: 5774
I think the best solution would be to store the dice in a list and then you can print each row of the dice with string formatting methods. An example of this would be:
import random
dice_list = [
[
"+-------+",
"| |",
"| * |",
"| |",
"+-------+"],
[
"+-------+",
"| * |",
"| |",
"| * |",
"+-------+"],
[
"+-------+",
"| * |",
"| * |",
"| * |",
"+-------+"],
[
"+-------+",
"| * * |",
"| |",
"| * * |",
"+-------+"],
[
"+-------+",
"| * * |",
"| * |",
"| * * |",
"+-------+"],
[
"+-------+",
"| * * * |",
"| |",
"| * * * |",
"+-------+"]
]
roll1 = random.randint(0, 5)
roll2 = random.randint(0, 5)
for i in range(5): # number of rows of strings per dice
print("{} {}".format(dice_list[roll1][i], dice_list[roll2][i]))
Which outputs something like this:
+-------+ +-------+
| * * * | | * * * |
| | | |
| * * * | | * * * |
+-------+ +-------+
Something to note though is the range of dice rolls with this method is 0-5 so if you wanted to say, print('You rolled a 2 and a 5')
you would have to add 1 to both roll1
and roll2
so the numbers will match up!
Upvotes: 2