Rolling Dice CodeHS. Please help :)
Hello Repel community,
I am stuck on a CodeHS question titled: Rolling Dice. Can you guys please help me with this, it means the world to me, thank you <3
Here is the problem :)
Write a python program that will print out all combinations that can be made when 2 dice are rolled.
Your output should look like this:
1, 1
1, 2
1, 3
1, 4
1, 5
1, 6
2, 1
2, 2
2, 3
2, 4
And should continue until all values up to 6, 6 are printed. (Hint: You should have a space after each comma!)
Because you know exactly how many times each loop should iterate, you should use nested for loops (a loop within a loop) to complete this program.
One sec I'll try to find a solution I'll ping you when I finish
@Codemonkey51
thank you, you're awesome :)
Your welcome @LostWonderer
The most efficient way I found was to just code a loop within a loop and have the value of each loop iteration print as a string:
for i in range(1,7):
for j in range(1,7):
print(str(i)+",",str(j))
No lists or functions needed! Printing the loop counter as a string let me concatenate the "," in the middle of the output, which makes CodeHS happy.
@LostWonderer found the solution, I am not going to tell you the code just some hints so that you will learn it not just copy (you can check your answer with the one on my profile named CodeHS) so you should make a list with numbers (in string form) 1 through 6 in order of least to greatest, so now how will you get it to print all the pairs all you will need to do is loop through the list multiple times printing the two roles with a for loop.
AKA: make a list containing
["1","2","3","4","5","6"]
then have one for loop running around the list, then another for loop inside now it should run through all the solutions, the final problem is to just print it assuming the first for loop uses I and the second uses j you just need to print a combination ofi
,,
, andj
then just print the output of thatEDIT: you can check the code here
@Codemonkey51
Thank you so much! Have a wonderful day :)
Your welcome, will do @LostWonderer