Python Warm / Cold Declaring Variable outside while loop
<p>I'm writing a simple warmer / colder number guessing game in Python.</p>

<p>I have it working but I have some duplicated code that causes a few problems and I am not sure how to fix it.</p>

<pre><code>from __future__ import print_function
import random

secretAnswer = random.randint(1, 10)

gameOver = False
attempts = 0
currentGuess = int(input("Please enter a guess between 1 and 10: "))
 originalGuess = currentGuess

while gameOver == False and attempts &lt;= 6:
    currentGuess = int(input("Please enter a guess between 1 and 10: "))
    attempts += 1

originalDistance = abs(originalGuess - secretAnswer)
currentDistance = abs(currentGuess - secretAnswer)

if currentDistance &lt; originalDistance and currentGuess != secretAnswer:
    print("Getting warmer")

elif currentDistance &gt; originalDistance:
    print("Getting colder")

if currentDistance == originalDistance:
    print("You were wrong, try again")

if currentGuess == secretAnswer or originalGuess == secretAnswer:
    print("Congratulations! You are a winner!")
    gameOver = True

if attempts &gt;= 6 and currentGuess != secretAnswer:
    print("You lose, you have ran out of attempts.")
    gameOver = True

print("Secret Answer: ", secretAnswer)
print("Original Dist: ", originalDistance)
print("Current Dist: ", currentDistance)
</code></pre>

<p>It asks for input before I enter the loop, which is to allow me to set an original guess variable helping me to work out the distance from my secret answer.</p>

<p>However, because this requires input before the loop it voids any validation / logic I have there such as the if statements, then requires input directly after this guess, now inside the loop. </p>

<p>Is there a way for me to declare originalGuess inside the loop without it updating to the user input guess each iteration or vice versa without duplicating currentGuess?</p>

<p>Thanks</p>

<p>There doesn't seem to be a need to ask the user before you enter the loop... You can just check if guesses = 1 for the first guess...</p>

<pre><code>gameOver=False
guesses = 0
while not gameOver:
    guesses += 1
    getUserInput
    if guesses = 1 and userInput != correctAnswer:
      print "try again!"  
    checkUserInput
print "good job!, it took you {} guesses!".format(guesses)
</code></pre>

python while-loop