[英]Can someone tell me what is wrong with my code
我是一名初學者編碼員,只想對您的幫助表示感謝。 我可能犯了一些新手錯誤,但這就是我來這里學習的原因。 我不知道出了什么問題,但可以說我插入了一個字符串 (50,K,C) 它應該將 50 開爾文轉換為攝氏度,但我的代碼什么也沒做
def temperature_converter(value, scale_from, scale_to):
if scale_from == "F" and scale_to == "C":
(value -32)* 5 / 9 for F to C
return
elif scale_from == "F" and scale_to == "K":
(value * 5 / 9) + 273 for F to K
return
elif scale_from == "C" and scale_to == "F":
value * (9 / 5) + 32 for C to F
return
elif scale_from == "C" and scale_to == "K":
(value + 273) for C to K
return
elif scale_from == "K" and scale_to == "F":
9/5 * (value - 273) + 32 for K to F
return
elif scale_from == "K" and scale_to == "C":
value - 273 for K to F
return
好的,讓我們一點一點地:
for F to C
和其他類似的“語句”,您在代碼中使用的不是有效的 Python。 另外,它們似乎沒有任何作用,所以我在下面刪除了它們。(value -32)* 5 / 9
,您必須將該結果存儲在某處。 value
將始終保持不變,除非您為其分配一個值。return
,你必須返回一些東西。 只是有return
關鍵字沒有做任何事情。這是您的代碼的修訂版本。 我特意把第一種和第二種情況(F到C,F到K)變得更加冗長,以說明第2點:
def temperature_converter(value, scale_from, scale_to):
if scale_from == "F" and scale_to == "C":
a = (value -32)* 5 / 9 #Assign the modified value to a variable
return a
elif scale_from == "F" and scale_to == "K":
value = (value * 5 / 9) + 273 #The value variable can be reused
return value
elif scale_from == "C" and scale_to == "F":
return value * (9 / 5) + 32 #Always return something. Don't have just the keyword
elif scale_from == "C" and scale_to == "K":
return value + 273
elif scale_from == "K" and scale_to == "F":
return 9/5 * (value - 273) + 32
elif scale_from == "K" and scale_to == "C":
return value - 273
補充幾點:
"f"
的可能性。最終代碼將是:
def temperature_converter(value, scale_from, scale_to):
scale_from = scale_from.upper() #Make all arguments upper case
scale_to = scale_to.upper()
if scale_from == "F": #Group cases together
if scale_to == "C":
return (value -32)* 5 / 9
elif scale_to == "K":
return (value * 5 / 9) + 273
elif scale_from == "C":
if scale_to == "F":
return value * (9 / 5) + 32
elif scale_to == "K":
return value + 273
elif scale_from == "K":
if scale_to == "F":
return 9/5 * (value - 273) + 32
elif scale_to == "K":
return value - 273
raise ValueError("Invalid argument") #Have predictable behavior if invalid input is given
當您在這里時,我們不妨提供正確的轉換公式
def temperature_converter(value, scale_from, scale_to) :
if scale_from == "F" and scale_to == "C" :
return (value - 32.0) * 5 / 9 #for F to C
elif scale_from == "F" and scale_to == "K":
return (value + 459.67) * 5 / 9 #for F to K
elif scale_from == "C" and scale_to == "F":
return value * (9.0 / 5.0) + 32 #for C to F
elif scale_from == "C" and scale_to == "K":
return value + 273.15 #for C to K
elif scale_from == "K" and scale_to == "F":
return ((value - 273.15) * 1.8) + 32 #for K to F
elif scale_from == "K" and scale_to == "C":
return value - 273.15 #for K to C
然后這樣調用
result = temperature_converter(50, "F", "C")
或這個
result = temperature_converter(50, 'F', 'C')
我認為您也可能錯誤地調用了該函數。
有一件非常重要的事情你可能不知道,這里還沒有人告訴你。 它可能會導致您在嘗試找出錯誤值的原因時頭疼。
有整數和浮點數(或floats )。 簡單地說,整數是整數,但浮點數可以使用小數和分數。
如果您只使用整數,它會將值四舍五入為整數。 我在攝氏到華氏的轉換中注意到了這一點。 當我使用 9 / 5 而不是 9.0 / 5.0 時,它的值四舍五入了很多,而我的答案很差。 這是因為它將 9 / 5 舍入為整數,而不是 9 / 5 = 1.8。
所以記住這一點。 確保您的 Python 解釋器以您期望的方式使用浮點數(十進制值),並且以您想要的方式進行數學運算。
我在所有公式中都包含了小數點,因此 Python 使用浮點數進行數學運算。 (至少我的 Python 解釋器是。)
另外,仔細檢查公式。
編輯:
不要忘記打印您的答案
print(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.