user2850734
user2850734

Reputation: 11

Infinite loop in python

I'm supposed to be writing code for the following problem:

A player rolls two six sided die. If the sum of the two dice is not seven, the sum is added to the player's total score and the player gets to roll again. When the player rolls a sum of seven, the game is over.

This is what I have so far:

def main():
  dice1 = randrange(1, 7,)
  dice2 = randrange(1, 7,)

  roll = dice1 + dice2
  score = 0
  count = 0
  while roll != 7:
    count = count + 1
    score = score + roll
    if roll == 7:
     break
  print count
  print score

main()

However, it just gives me an infinite loop when it should be rolling the die only until the sum of the die is seven.

How do I fix it?

Upvotes: 0

Views: 322

Answers (4)

boates
boates

Reputation: 127

Here is an elegant solution to your problem:

from random import randint

count, score = 0, 0
while True:
    roll = randint(1,6) + randint(1,6)
    if roll == 7:
        break
    count += 1
    score += roll

print count, score

Upvotes: 0

user2555451
user2555451

Reputation:

You need to update the value of roll with each iteration. Right now, roll never changes so it will never equal 7 (meaning, the loop will run forever).

I think you want your code to be this:

from random import randrange
def main():

  # Note that the following two lines can actually be made one by doing:
  # score = count = 0
  score = 0
  count = 0

  # There is really no need to define dice1 and dice2
  roll = randrange(1, 7,) + randrange(1, 7,)

  # This takes care of exiting the loop when roll = 7
  # There is no need for that if block
  while roll != 7:

    # Note that this is the same as count = count + 1
    count += 1

    # And this is the same as score = score + roll
    score += roll

    # Update the value of roll
    # This is actually the line that fixes your problem
    roll = randrange(1, 7,) + randrange(1, 7,)

  print count
  print score

main()

Also, note that I made some changes to the script to improve efficiency.

Upvotes: 1

gefei
gefei

Reputation: 19766

You have to get new numbers of your dice in the while loop

def main():
      dice1 = randrange(1, 7,)
      dice2 = randrange(1, 7,)

      roll = dice1 + dice2
      score = 0
      count = 0
      while roll != 7:
        count = count + 1
        score = score + roll
        if roll == 7:
          break
        dice1 = randrange(1, 7,)
        dice2 = randrange(1, 7,)
        roll = dice1 + dice2
      print count    
      print score

main()

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121276

You are not updating roll; make sure you roll again in the loop:

roll = randrange(1, 7) + randrange(1, 7)
score = count = 0

while roll != 7:
    score += roll
    count += 1
    # re-roll the dice for the next round
    roll = randrange(1, 7) + randrange(1, 7)

Once you assigned dice1 + dice2 to roll, it does not by itself 're-roll' the dice after each loop.

Upvotes: 2

Related Questions