简体   繁体   中英

How to print random result multiple times using 'while loop' or other methods?

I am trying to play a game of rolling two dice. The game consists of 500 rounds. In each round, two coins are being rolled at the same time, if both coins have 'heads' then we win £1, if both have 'tails' then we lose £1 and if we have a situation where one coin shows 'heads' and the other coin shows 'tails' or vice versa then we just 'try again'.I am trying to print the final result 20 times, however the 'while loop' does not seem to be working, where am I going wrong?

coin_one = [random.randint(0, 1) for x in range(500)] #flips coin 1 heads or tails 500 times
coin_two = [random.randint(0, 1) for x in range(500)] #flips coin 2 heads or tails 500 times
game = zip(coin_one, coin_two) 

def coin_toss_game1():    
    total = 0
    for coin_one, coin_two in game:
        if coin_one and coin_two:
            total += 1   #if both coin one and two return heads then we win $1
        elif not a and not b:
            total -= 1   #if both coin one and two return tails then we lose $1
        else:
            continue #if coin one returns heads and coin two returns tails then we try again
    return total

y = 0
while y < 21:
print(coin_toss_game1())  #This is where I am encountering problems with printing result 20 times
y += 1

This is giving a result of: 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Its supposed to return a result of something like: 7 2 -5 15 -9 12 ... what mistake am I making here?

You have two issues here.

1. You're trying to reuse a zip object

In Python 3 (which I assume you're using based on your result), zip() returns a generator. A generator can only be iterated over once. If you try to iterate over it again, it will be empty. This is why all results beyond the first come back as zero... they don't actually do anything.

If you only fix, this, though, you'll still have a problem... all 20 runs will produce the same result.

2. You're using the same sequence of coin flips for all iterations

You generated your coin flips once at the top of the program, and then tried to use the same sequences for all 20 rounds. It shouldn't be surprising that all results will be the same.

The fix for both issues is the same: move the generation of coin flips and zip ping into the function so that it gets redone each time you run it:

def coin_toss_game1():
    coin_one = [random.randint(0, 1) for x in range(500)]
    coin_two = [random.randint(0, 1) for x in range(500)]
    game = zip(coin_one, coin_two)
    total = 0
    for coin_one, coin_two in game:
        ...

As an aside, a simpler Python idiom for looping a fixed number of times is:

for _ in range(20):    # Nothing magical about the underscore... it's just a dummy variable
    print(coin_toss_game1())

I think this is the code you're looking for:

def coin_toss_game1():
  coin_one = [random.randint(0, 1) for x in range(500)]
  coin_two = [random.randint(0, 1) for x in range(500)]
  game = zip(coin_one, coin_two)
  total = 0
  for a, b in game:
    if a and b:
      total += 1
    elif not a and not b:
      total -= 1
    else:
      continue
  return total

y = 0
while y < 21:
  print(coin_toss_game1())
  y += 1

The code you have runs the game once (500 rounds) instead of 20 times. The code above, with each iteration of the while loop, runs the 500 rounds and displays the results. So you get the result of a 500-round game, 20 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM