title: how to prevent infinite while loop in python 3
body: <p>I am creating a program that plays the game pig dice. It should do the following:
  1. Select whether the user or the computer plays the first turn.
  2. If computer starts first, computer plays one turn
  3. Repeat as long as computer score is less than 100:
  a. User plays one turn.
  b. If user’s score reaches 100, exit loop
  c. Computer plays one turn.
  4. Print results.
  My problem is that when I run the program, there is an infinite loop, and I am not sure where it is running into this problem. Could anyone point out where the problem is occurring and possibly make suggestions as to how to prevent this? Pointing out any other errors is also appreciated as I am new to python.</p>
  
  <pre><code>from random import randint
  
  
  def print_scores(uscore, cscore):
      uscore = 0
      cscore = 0
  
  
      print(uscore, cscore)
  
  
  def roll_two_dice():
      roll1 = randint(1, 6)
      roll2 = randint(1, 6)
  
      if roll1 or roll2 == 1:
          return 0
  
  
      elif roll1 and roll2 == 1:
          return 25
      else:
          return roll1 + roll2
  
  
  def computerTurn(score):
  
  
      turn_score = 0
      while score + turn_score &lt; 100 and turn_score &lt; 20:
          roll = roll_two_dice()
          if roll == 0:
              return 0
          score = roll + turn_score
      return turn_score
  
  
  def userTurn(score):
      turn_score = 0
      while score + turn_score &lt; 100 or user == hold:
          roll = roll_two_dice()
          if roll == 0:
              return 0
          score = roll + turn_score
      return turn_score
  
  
  def final_result(uscore, cscore):
      uscore = 0
      cscore = 0
      print('Final results:{}'.format(uscore, cscore))
      if uscore &gt;= 100:
          print('You win')
      else:
          print('Computer wins')
  
  
  def main():
      c_score = 0
      u_score = 0
      if randint(0, 1) == 0:
          print('Computer starts')
          c_turn = computerTurn(c_score) + c_score
      while c_score &lt; 100:
          print('Current scores')
          print_scores(u_score, c_score)
          u_turn = userTurn(u_score) + u_score
          if u_score &gt;= 100:
              exit
          c_turn = computerTurn(c_score)
          print(c_turn)
          c_score = c_score + c_turn
      final_result(u_score, c_score)
  
  
  main()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/XHnBn.jpg" rel="nofollow noreferrer">codepart1</a></p>
  
  <p><a href="https://i.stack.imgur.com/NVQPL.jpg" rel="nofollow noreferrer">codepart2</a></p>