2016-01-24 171 views
2

我試圖創建一個攝氏溫度到華氏溫度轉換的攝氏溫度轉換範圍,從0到100的增量爲0.5。這是我迄今爲止,但我似乎無法讓循環正確運行,因爲它以攝氏開始:0 fahrenheit:0;我需要它以攝氏開始:0華氏度:32(正確的轉換)。列表中的攝氏溫度到華氏溫度循環

count = 0 
celsius = 0 
while (celsius <= 100): 
    print ('Celsius:', celsius, 'Fahrenheit:', count) 
    celsius = celsius + 0.5 
    count = (((celsius)*9/5)+32) 

回答

1

我認爲你正在尋找更重要的是這樣的:

celsius = 0 
while celsius <= 100: 
    fahrenheit = celsius * 9.0/5.0 + 32 
    print ('Celsius:', celsius, 'Fahrenheit:', fahrenheit) 
    celsius += 0.5 
2

你爲什麼不寫一個函數?

def toFarenheit(celsius): 
    return (9.0/5.0) * celsius + 32 

def toCelsius(farenheit): 
    return (farenheit - 32) * (5.0/9.0) 
# I don't actually use this method, but it's still good to have 

然後,你可以這樣做:

for y in range(0,200): 
    x = y/2.0 
    print("Celsius: ", x, ", Farenheit: ", toFarenheit(x)) 
+1

範圍不浮動 – Copperfield

+0

由於工作,修復。 – AMACB

相關問題