2017-06-13 20 views
-1
print("Please think of a number between 0 and 100!") 
low = 0 
high = 100 
ans = (low + high)/2 
while True: 
    print("Is your secret number %d " %ans) 
    userguess = input("Enter 'h' to indicate the guess is too high. Enter 
    'l' to indicate the guess is too low. Enter 'c' to indicate I guessed 
    correctly. ") 
    if userguess == 'h': 
     high = ans 
    elif userguess == 'l': 
     low = ans 
    elif userguess == 'c': 
     break 
    else : 
     print("Sorry, I did not understand your input.") 
     continue 
    ans = (low + high)/2 

print("Game over. Your secret number was: %d" %ans) 

當數字是42和91時,它不會按照它應該的方式工作。這段代碼有什麼問題。我的程序是關於猜測蟒蛇中0到100之間的數字

+1

究竟什麼不行?它怎麼錯了? – poke

+0

使用'/ /'而不是'/' – khelwood

回答

0

要回答你的問題,沒有什麼是錯的代碼。已經嘗試了您提到的號碼,並且它正常工作。

根據Python 3.x文檔,您正在正確使用printf樣式函數。對於這種類型的轉換的唯一不可選組件是

  1. 的「%」字符,這標誌着說明符的開始。

因此,您正在強制將浮點轉換爲「帶符號的整數小數」,以便將其正確打印給用戶。

一切都是正確的,在我的最後,一切工作正常。

+0

因此我在edx上做了一門計算機科學課程,這是個問題,它運行完美,但唯一的問題是使用整數轉換 –

0

你得到中間浮點值時,你應該使用整數運算只