2015-10-26 97 views
0

因此我建立了一個簡單的while循環來檢查冷卻茶需要多少吹。一擊將茶溫降低10℃。問題是我不知道如何繼續這個。我知道它很簡單,但剛開始使用python。由於計算循環使用次數

tea = 100 #temperature of tea to start with 
while tea >= 70: 
    print (str(tea) + " C") 
    tea = tea - 10 
print (" It's ready now ... ") 
這樣做將有外循環的變量,並在每次迭代增加它
+0

爲什麼不能在循環之前從0開始計數,並以1爲每個「一擊」增加了嗎? – mgilson

回答

1

的一種方式。所以:

tea = 100 
count = 0 
while tea >= 70: 
    print(str(tea) + " C") 
    tea -= 10 # shorthand for tea = tea-10 
    count += 1 
print("It's ready now") 
print("It took {} blows to cool down".format(count)) 
+0

哦謝謝,我試圖使用計數功能,但我以錯誤的方式顯示。非常感謝你 – user3077730

+0

yup,很高興幫助:) –

+0

和{}在最後一次打印中的含義是什麼?這是否意味着它會顯示.format(count)中的任何內容? – user3077730

1
tea = 100 #temperature of tea to start with 
count = 0 
while tea >= 70: 
    print (str(tea) + " C") 
    tea = tea - 10 
    count += 1 
print (" It's ready now ... ") 
0

爲什麼你需要一個循環?

def steps(current, target, step): 
    return int(math.floor((current - target)/float(step)) + 1) 

print("It took {} steps!".format(steps(100, 70, 10))) 
+1

以及即時通訊學習python循環,所以這就是爲什麼:s – user3077730

+0

@ user3077730偉大的:)歡迎! – MostafaR