Home > database >  Need coding help using "zip()" function
Need coding help using "zip()" function

Time:01-30

I am working on a code for a class that I am taking. I have finished everything but the last step and was looking for some guidance or advice on how to finish it. I have included the directions and my code so far below:

Directions:

Generate two random lists with 1000 random integers between 1 and 6 called die1 and die2. Loop through the two lists (using zip) to return the dice in pairs (as if you rolled them) and count:

  • The number of 7's (add both die) rolled
  • The number of 11's (add both die). rolled
  • The number of "snake eyes" rolled
  • The number of pairs rolled

My Code:


# die1 list
die1 = [] # create an empty list
while len(die1) < 1000: 
    x = random.randint(1,6) # generate a random integer between 1 & 6
    die1.append(x)
print(die1)
print()

# die2 list
die2 = [] # create an empty list
while len(die2) < 1000: 
    x = random.randint(1,6) # generate a random integer between 1 & 6
    die2.append(x)
print(die2)
print()

# Zipping die1 & die2 into pairs
zipped = zip(die1,die2)
print(zipped)
print()

# Adding the Zipped Numbers Together
sum = [x y for (x,y) in zipped]
print(sum)
print()

# Number of 7's rolled
counter1 = sum.count(7)
print('Appearances made by 7: ')
print(counter1)
print()

# Number of 11's rolled
counter2 = sum.count(11)
print('Appearances made by 11: ')
print(counter2)
print()

# Number of Snake Eyes rolled
counter3 = sum.count(2)
print('Appearances made by 2: ')
print(counter3)
print()

# Number of Pairs rolled

Thank you!

CodePudding user response:

You need to count have many times, x==y when zipping the 2 lists

counter4 = sum(1 for x, y in zip(die1, die2) if x == y)

Suggestions

  • use one for loop to fill both arrays
  • don't use builtin function name sum to one of your variable
  • loop once over the zip and compute all of your counter* : using list.count() will iterate the values each time
die1 = []
die2 = []
for _ in range(1000):
    die1.append(random.randint(1, 6))
    die2.append(random.randint(1, 6))

counter1, counter2, counter3, counter4 = 0, 0, 0, 0
for x, y in zip(die1, die2):
    die_sum = x   y
    if die_sum == 7:
        counter1  = 1
    elif die_sum == 11:
        counter2  = 1
    elif die_sum == 2:
        counter3  = 1

    if x == y:
        counter4  = 1

print('Appearances made by  7:', counter1)
print('Appearances made by 11:', counter2)
print('Appearances made by  2:', counter3)
print('Appearances of pairs: ', counter4)

CodePudding user response:

How about something like this?

import random

die1 = [random.randint(1, 6) for _ in range(1000)]
die2 = [random.randint(1, 6) for _ in range(1000)]

occurences = {}

die_zipped = zip(die1, die2)

for pair in die_zipped:
    pair_sum = sum(pair)
    occurences[pair_sum] = occurences.get(pair_sum, 0)   1

total_pairs = sum(occurences.values())

print(f"{occurences[7] = }\n{occurences[11] = }\n{occurences[2] = }\n{total_pairs = }")

Output:

occurences[7] = 152
occurences[11] = 47
occurences[2] = 24
total_pairs = 1000

CodePudding user response:

Hey i liked this challenge !

What about this code ?

from random import randint
list1,list2 =[ [randint(1,6) for i in range(1000)] for j in range(2)]
sums = [[item1 item2,item1==item2] for item1,item2 in zip(list1,list2)]
print("Snakes eyes",len([s for s in sums if s[0]==2]))
print("Pairs",len([s for s in sums if s[1]==True]))
print("Sevens",len([s for s in sums if s[0]==7]))
print("Elevens",len([s for s in sums if s[0]==11]))


pairs = list(zip(list1,list2))
print("Pairs",pairs)

Sample results :

Snakes eyes 32
Pairs 176
Sevens 145
Elevens 50
Snakes eyes 26
Pairs 151
Sevens 164
Elevens 57
  •  Tags:  
  • Related