Bug with a program for a guessing game
<p>I am creating a program where a user had to guess a random number in a specific range and has 3 tries to do so. After each try, if the guess is correct you tell the user they won and if the guess is wrong you tell them its wrong and how many tries they have got remaining. When the number of tries exceed 3 without a correct guess, you tell the user they are out of tries and tell them they lost. When the program finishes after a correct or 3 wrong tries and the game is over, you ask if the user wants to play again. If they say "yes", you star the game again and if they say "no", you end the game.
This is the code I have come up with:</p>

<pre><code>import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries&lt;=3:
        inp = input("Guess the number: ")
        if tries&lt;3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries&gt;2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
    print "Okay thanks for playing!"
elif again =="yes":
    print "Great!"
</code></pre>

<p>Now when you guess the correct number before 3 tries, the program seems to be working fine, but even when the number of wrong tries exceeds 3, the program keeps asking for more guesses but I want it to stop the program right there. The second thing I don't get is how do I start the whole game again when the user wants to play again? 
How can I modify my code to fix these errors?</p>

<p>The simplest way to stop a loop when some condition becomes true is to use <code>break</code>. So when the player guesses correctly, the code would look like</p>

<pre><code>if inp == x:
    guess = True
    print "Correct! You won"
    break
</code></pre>

<p>Additionally, you should break the loop when you run out of guesses, otherwise it will keep running</p>

<pre><code>else:
    print "Wrong guess! You are out of tries"
    print "Game Over!"
    break
</code></pre>

<p>To let the player play again, you can have a variable <code>playing</code> and use it in a while loop that wraps the whole game, and when they say they don't want to play anymore, make it <code>False</code> so that the outer loop ends.</p>

<pre><code>playing = True
while playing:
    x = round(random.random()*5)
    guess = False
    tries = 0
    while guess != True:
        ...

    again = raw_input("Do you want to play again? ")
    if again == "no":
        print "Okay thanks for playing!"
        playing = False
    elif again =="yes":
        print "Great!"
</code></pre>

<p>As you noted in comments on another answer, when the player gets their third guess wrong, the game tells them they have 0 guesses left and to try again, and then tells them they are out of tries and that the game is over. Since you want it to only tell them they are out of tries and that the game is over, you can change the main block of code to:</p>

<pre><code>while guess != True:
    inp = input("Guess the number: ")
    if inp == x:
        guess = True
        print "Correct! You won"
    else:
        tries = tries + 1
        if tries == 3:
            print "Wrong guess! You are out of tries"
            print "Game Over!"
            break
        else:
            print "Wrong guess! Try again."
            print "Your remaining tries are",(3-tries)
</code></pre>

python python-2.7