我正在寫一個程序,應該猜測用戶的祕密號碼使用平分搜索。我覺得我理解二分法搜索的概念相當好,但是我的IDE(Canopy)不會讓我「運行」代碼,我認爲這是由於錯誤或它在我運行之前要做的事情。使用二分搜索猜數
lowend = 1
highend = 100
guess = 50
choice = 0
print "Please think of a number between 0 and 100!"
while choice != 'c':
print "Is your secret number " + str(guess) + "?"
print "Enter 'h' to indicate the guess is too high.",
print "Enter 'l' to indicate the guess is too low.",
choice = raw_input("Enter 'c' to indicate I guessed correctly.")
if choice == 'c':
break
elif choice == 'h':
highend = guess
guess = (highend + lowend)/2
print 'Is your secret number ' + str(guess) + "?"
choice = 0
elif choice == 'l':
lowend = guess + 1
guess = (highend + lowend)/2
print 'Is your secret number ' + str(guess) + "?"
else:
print "Sorry, I did not understand your input."
choice = 0
print 'Your number is: ' + str(guess) + "."
我不知道是否有什麼我做錯了,但樹冠的綠色「運行」按鈕是灰色。爲什麼會發生?有人看到我的代碼顯然有什麼問題嗎?
我不是一個蟒蛇開發,但你沒有一個主要的方法。 –
@SilviuBurcea在Python中無關 – jonrsharpe
前四行不正確縮進;也許Canopy阻止你嘗試運行帶有語法錯誤的代碼?您可以隨時嘗試運行腳本(IDLE /命令行)來查看。 – jonrsharpe