2016-10-12 7 views
0

Pythonで単純な暖かい/寒い数字の推測ゲームを書いています。Pythonウォーム/コールド宣言変数whileループwhile while

私はそれを動作させていますが、いくつかの問題を引き起こす重複したコードがあり、それを修正する方法がわかりません。

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 <= 6: 
    currentGuess = int(input("Please enter a guess between 1 and 10: ")) 
    attempts += 1 

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

if currentDistance < originalDistance and currentGuess != secretAnswer: 
    print("Getting warmer") 

elif currentDistance > 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 >= 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) 

私はループに入る前に、それは私が私が私の秘密の答えからの距離を動作するように支援し、元の推測変数を設定することができるようになる、入力を要求します。

しかし、これはループの前に入力が必要なため、if文などの検証/ロジックを無効にして、この推測の直後に入力する必要があります。

originalGuessをループ内に宣言する方法はありますか?currentGuessを複製せずに、各繰り返しを推測するユーザー入力に更新することなく、またはその逆にします。

おかげ

答えて

1

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) 
...推測= 1場合は、ちょうど最初の推測を確認することができます...あなたがループに入る前に、ユーザーに依頼する必要があるように思えません。
+0

もし私がそれを行うならば、originalDistanceとcurrentDistanceはoriginalGuessの格納に依存しているので、どのように区別することができますか?どのように私はループ内のユーザー入力の各反復で更新されると宣言した場合 – FrasKyl