How can I make a while loop only run a limited number of times?
<p>In the code below, I'd like to run the function <code>roll()</code>, after: <code>deal = input("Your Turn: ")</code>, but only up to a limited number of cycles (52)</p>

<p>How can I achieve that?</p>

<pre><code>import random

ydeal = random.randint(1,15)
adeal = random.randint(1,15)

yscore = 0
ascore = 0

def roll():
    if deal == "Deal":
        print(ydeal, adeal)
        if ydeal &gt; adeal:
            yscore + 1
        elif ydeal &lt; adeal:
            ascore + 1
        print(yscore, ascore)

deal = input("Your Turn: ")
roll()
</code></pre>

<p>As a side note: I noticed that when printing <code>yscore</code> and <code>ascore</code> the value does not change during the loop, how can I fix that?</p>

<p>I noticed a couple of problems in your code:</p>

<ol>
<li>Inside the <code>roll()</code> function you have to declare that you want to use the global variables and not two new variables that are local to the function,</li>
<li>You are not assigning the increment to the value. A correct way to do it is: <code>yscore += 1</code>.</li>
</ol>

<p>At the end, I report to you a reviewed version of your code. At the end of the code I have inserted a 52 iterations while loop example.</p>

<pre><code>import random

ydeal = random.randint(1,9)
adeal = random.randint(1,9)

yscore = 0
ascore = 0

def roll():
    #Notify that the variables you want to use are not defined inside the function
    global yscore
    global ascore
    if deal == "!":
        print(ydeal)
        print(adeal)
        if ydeal &gt; adeal:
            yscore += 1
        elif ydeal &lt; adeal:
            ascore += 1
        print(yscore, ascore)

deal = input("Your Turn: ")
roll()

#while loop
max_iteration = 52
counter = 0

while counter &lt; max_iteration :
  counter += 1
  print ("while: ", counter)
</code></pre>

python