0

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?

3
  • 1
    you are not changing the value of roll in your while loop. no wonder it ends up in infinite loop Commented Oct 6, 2013 at 0:33
  • Also, you don't need if roll == 7: break That is taken care of by while roll != 7: Commented Oct 6, 2013 at 0:35
  • print ( (lambda f: f (f, 0, 0, 0, __import__ ('random') ) ) (lambda f, r, c, s, i: (c, s) if r == 7 else f (f, i.randint (1, 6) + i.randint (1, 6), c + 1, s + r, i) ) ) Commented Oct 6, 2013 at 5:29

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I would set roll=0 at the top just to keep the logic of rolling all in one place (at the end of the loop). It's one extra loop, but it isn't costly in term of performance.
Sure, but then you'd have to set count to -1 as well.
1

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.

Comments

0

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()

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.