我是Python的新手。 這是我的代碼,執行二進制搜索找到猜測的數字。我無法正確地弄清楚如何使我的代碼工作。 任何幫助,將不勝感激。謝謝。基本Python代碼不工作
print"Please think of a number between 0 and 100!"
guessed=False
while not guessed:
lo=0
hi=100
mid=(lo+hi)/2
print'Is you Secret Number'+str(mid)+'?'
print"Enter 'h' to indicate the guess is too high.",
print"Enter'l' to indicate the guess is too low",
print"Enter'c'to indicate I guessed correctly"
x=raw_input()
if(x=='c'):
guessed=True
elif(x=='h'):
#Too High Guess
lo=mid+1
elif(x=='l'):
lo=mid-1
else:
print("Sorry, I did not understand your input.")
print'Game Over','Your Secret Number was'+str()
這將是對你有幫助 - http://stackoverflow.com/questions/30934834/python-numbers -game-reverse/30935878#30935878 –
這有幾個錯誤。首先,每次在循環開始時將「lo」重置爲零,因此後面設置的任何內容都會變回零。將'lo = 0'移到循環之前。無論如何,這是一個開始,儘管如此。另請參閱https://www.python.org/dev/peps/pep-0008/ – cdarke
@VivekSable謝謝。 –