0
我正在寫一個簡單的溫暖/冷猜數字在Python遊戲。Python的冷/熱變聲明外,同時循環
我有工作,但我有導致一些問題的一些重複的代碼,我不知道如何解決它。
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每次迭代,反之亦然?
感謝
如果我這樣做,雖然,我怎麼能因爲它們依賴於存儲originalGuess區分originalDistance和currentDistance?如果我在循環內聲明,將會隨用戶輸入的每次迭代而更新 – FrasKyl