Search code examples
pythonlistdice

How would I make this Python program with lists/arrays Instead of multiple variables?


I got this to work but am terrible at making lists/calling them. Would anyone be able to compact this into list form? (Not for homework, finished already, just for learning opportunity/practice, so no hurry.) I assume my count variable can be made into a list and called in the for loop and during the final prints.

This was for a dice game that tracked how many times a number came up after 2 dice were thrown and added together. Pretty easy, I just suck at understanding lists, so explanations with code would be GREATLY apricated, my textbook does not expound on this topic enough. First post on stack, excuse any messed up jargon or rules. Thanks!

import random

count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
count8 = 0
count9 = 0
count10 = 0
count11 = 0
count12 = 0

rolls = int(input("How many times would you like to roll? "))



for i in range(rolls):

    die1 = random.randint(1, 6)
    print("Roll number 1 = " + str(die1))
    die2 = random.randint(1, 6)
    print("Roll number 2 = " + str(die2))
    total = die1 + die2
    print(total)


    if total == 2:
        count2 += 1
    elif total == 3:
        count3 += 1
    elif total == 4:
        count4 += 1
    elif total == 5:
        count5 += 1
    elif total == 6:
        count6 += 1
    elif total == 7:
        count7 += 1
    elif total == 8:
        count8 += 1
    elif total == 9:
        count9 += 1
    elif total == 10:
        count10 += 1
    elif total == 11:
        count11 += 1
    elif total == 12:
        count12 += 1


print("You rolled " + str(count2) + " 2's")
print("You Rolled " + str(count3) + " 3's")
print("You Rolled " + str(count4) + " 4's")
print("You Rolled " + str(count5) + " 5's")
print("You Rolled " + str(count6) + " 6's")
print("You Rolled " + str(count7) + " 7's")
print("You Rolled " + str(count8) + " 8's")
print("You Rolled " + str(count9) + " 9's")
print("You Rolled " + str(count10) + " 10's")
print("You Rolled " + str(count11) + " 11's")
print("You Rolled " + str(count12) + " 12's")

Solution

  • Modified your code little bit and I think if you use dictionary than it will be easy to use and understand.

    import random
    
    # count2 = 0
    # count3 = 0
    # count4 = 0
    # count5 = 0
    # count6 = 0
    # count7 = 0
    # count8 = 0
    # count9 = 0
    # count10 = 0
    # count11 = 0
    # count12 = 0
    
    count = {i: 0 for i in range(2,13)}
    
    rolls = int(input("How many times would you like to roll? "))
    
    
    
    for i in range(rolls):
    
        die1 = random.randint(1, 6)
        print("Roll number 1 = " + str(die1))
        die2 = random.randint(1, 6)
        print("Roll number 2 = " + str(die2))
        total = die1 + die2
        print(total)
    
    
    
        # if total == 2:
        #     count2 += 1
        # elif total == 3:
        #     count3 += 1
        # elif total == 4:
        #     count4 += 1
        # elif total == 5:
        #     count5 += 1
        # elif total == 6:
        #     count6 += 1
        # elif total == 7:
        #     count7 += 1
        # elif total == 8:
        #     count8 += 1
        # elif total == 9:
        #     count9 += 1
        # elif total == 10:
        #     count10 += 1
        # elif total == 11:
        #     count11 += 1
        # elif total == 12:
        #     count12 += 1
    
        count[total] += 1
    # print("You rolled " + str(count2) + " 2's")
    # print("You Rolled " + str(count3) + " 3's")
    # print("You Rolled " + str(count4) + " 4's")
    # print("You Rolled " + str(count5) + " 5's")
    # print("You Rolled " + str(count6) + " 6's")
    # print("You Rolled " + str(count7) + " 7's")
    # print("You Rolled " + str(count8) + " 8's")
    # print("You Rolled " + str(count9) + " 9's")
    # print("You Rolled " + str(count10) + " 10's")
    # print("You Rolled " + str(count11) + " 11's")
    # print("You Rolled " + str(count12) + " 12's")
    
    for i in range(2,13):    
        print("You rolled " + str(count[i]) + " "+i+"'s")