[英]Can someone tell me what is wrong with my code
I am a beginner coder and would just like to say thanks for the help.我是一名初学者编码员,只想对您的帮助表示感谢。 I probably made some rookie mistakes but that's why I'm here to learn.
我可能犯了一些新手错误,但这就是我来这里学习的原因。 I cant figure out what's wrong but lets say I plug in a string (50,K,C) it should convert 50 Kelvin to Celsius but my code does nothing
我不知道出了什么问题,但可以说我插入了一个字符串 (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
Okay, lets take it bit by bit:好的,让我们一点一点地:
for F to C
and other similar "statements" you have in your code are not valid python. for F to C
和其他类似的“语句”,您在代码中使用的不是有效的 Python。 Plus, they seem to serve no purpose, so I have removed them below.(value -32)* 5 / 9
, you must store that result somewhere.(value -32)* 5 / 9
,您必须将该结果存储在某处。 value
will remain the same throughout, unless you assign a value to it. value
将始终保持不变,除非您为其分配一个值。return
, you have to return something .return
,你必须返回一些东西。 Just having the return
keyword there doesn't do anything.return
关键字没有做任何事情。 Here is a revised version of your code.这是您的代码的修订版本。 I have deliberately made the first and second case (F to C, F to K) more verbose to illustrate point 2:
我特意把第一种和第二种情况(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
A couple of extra points:补充几点:
"f"
in lowercase into account in this case."f"
的可能性。 Final code would be:最终代码将是:
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
We might as well give to the correct conversion formulas while you're here当您在这里时,我们不妨提供正确的转换公式
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
Then call it like this然后这样调用
result = temperature_converter(50, "F", "C")
or this或这个
result = temperature_converter(50, 'F', 'C')
I think you might have been calling the function wrong, as well.我认为您也可能错误地调用了该函数。
There is a very important thing that you might not know, and nobody here told you yet.有一件非常重要的事情你可能不知道,这里还没有人告诉你。 It could potentially cause you major headaches trying figure out why you are getting the wrong values.
它可能会导致您在尝试找出错误值的原因时头疼。
There are integers and floating point numbers (or floats ).有整数和浮点数(或floats )。 To put it simply, integers are whole numbers, but floats can use decimals and fractional values.
简单地说,整数是整数,但浮点数可以使用小数和分数。
If you use only integers it will round the values to a whole number.如果您只使用整数,它会将值四舍五入为整数。 I noticed this in a big way with the Celsius to Fahrenheit conversion.
我在摄氏到华氏的转换中注意到了这一点。 When I used 9 / 5 instead of 9.0 / 5.0, it rounded the value a lot, and my answer was way off.
当我使用 9 / 5 而不是 9.0 / 5.0 时,它的值四舍五入了很多,而我的答案很差。 This is because it was rounding 9 / 5 to a whole number, instead of 9 / 5 = 1.8.
这是因为它将 9 / 5 舍入为整数,而不是 9 / 5 = 1.8。
So keep that in mind.所以记住这一点。 Make sure your Python interpreter is using floats (decimal values) in the way you're expecting, and it's doing the math the way you want.
确保您的 Python 解释器以您期望的方式使用浮点数(十进制值),并且以您想要的方式进行数学运算。
I included decimal points in all the formulas, so Python was using floats for the math.我在所有公式中都包含了小数点,因此 Python 使用浮点数进行数学运算。 (At least my Python interpreter was.)
(至少我的 Python 解释器是。)
Also, double check the formulas.另外,仔细检查公式。
Edit:编辑:
Don't forget to print your answer不要忘记打印您的答案
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.