My code isnt outputting the expected output
我的代码没有输出预期的输出
The code consists of a module file (degree.py) and 6 other files that run the module and output a temperature in (C, F or K) from user input in (C, F, K)
in other words, the 6 files converts between the different degrees respectively
Heres the module file (degree.py):
该代码由一个模块文件(egree.py)和6个其他文件组成,这些文件运行模块并根据用户在(C,F,K)中的输入输出以(C,F或K)为单位的温度。换句话说,这6个文件分别在不同的度数之间进行转换。以下是模块文件(egree.py):
#degrees (Celsius, Fahrenheit, Kelvin)
bruh = float(0)
c_1 = float((5/9) * (float(bruh) - 32)) #F to C
c_2 = float((float(bruh) - 273.15) * 1) #K to C
f_1 = float(((9/5) * float(bruh)) + 32) #C to F
f_2 = float((float(bruh)-273.15) * (9/5) + 32) #K to F
k_1 = float((float(bruh) + 273.15) * 1) #C to K
k_2 = float((float(bruh)-32) * (5/9) + 273.15) #F to K
And heres one of the 6 files (fahrenheitToCelsius.py):
以下是6个文件之一(fahrenheitToCelsius.py):
import degree
test = False
while test == False:
bruh = input("Enter a positive numerical value: ")
bruh.isnumeric()
if bruh.isnumeric() == True:
print("If the temperature is", bruh, "degrees in Fahrenheit, then it is", degree.c_1, "degrees in Celsius.")
print()
print("Do acknowledge that the calculations done here in Python alone are not going to be precise.")
test = True
else:
print("This was not a numerical value bruh :(")
test = False
As the name suggests, this specific code was supposed to convert a value from degree F to C.
顾名思义,这个特定的代码应该是将一个值从F度转换为C度。
However I am noticing that the outputs are incorrect, first I thought it was a float margin error, but I soon to find out the culprit could be from the value bruh
somehow becoming 0
然而,我注意到,输出是不正确的,首先,我认为这是一个浮动保证金错误,但我很快就发现罪魁祸首可能是从价值bruh不知何故成为0
I would really like to be enlightened into why the code isn't working. I would really appreciate it if someone could help me solve this problem.
我真的很想知道为什么代码不起作用。如果有人能帮我解决这个问题,我将不胜感激。
更多回答
In your own words: if I want to make a peanut butter sandwich, and I know how to do two things: spread peanut butter on bread, and take bread out of a bag, which should I do first? So, the same thing here. If I want to, say, calculate the celcius equivalent of some fahrenheit number, which should I do first: apply the conversion formula to the number, or decide what the number is? You should make sure you understand these kinds of fundamentals about writing a program, before trying to work with modules.
用你自己的话说:如果我想做花生酱三明治,我知道怎么做两件事:把花生酱涂在面包上,然后从袋子里拿出面包,我应该首先做哪一件?所以,这里也是一样的。比方说,如果我想要计算某个华氏数值的Celcius当量,我应该首先做什么:对这个数字应用转换公式,还是决定这个数字是什么?在尝试使用模块之前,您应该确保了解编写程序的这些基础知识。
You should consider a radical redesign of degree.py Perhaps develop a class with methods for converting the various temperature scales
您应该考虑彻底重新设计Degree.py,或许可以开发一个带有转换各种温度标度的方法的类
优秀答案推荐
Because you assign bruh = float(0) in degrees.py
因为您将breh=Float(0)赋值为度.py
#degrees (Celsius, Fahrenheit, Kelvin)
bruh = float(0)
c_1 = float((5/9) * (float(bruh) - 32)) #F to C
in degrees.py, bruh is always 0 and does not take the input value.
在Degrees.py中,breh始终为0,不接受输入值。
you should def a function in degree.py, like:
您应该在define.py中定义一个函数,如下所示:
def f_2_c(bruh):
c_1 = float((5/9) * (float(bruh) - 32))
return c1
and use in ahrenheitToCelsius.py
并在ahrenheitToCelsius.py中使用
if bruh.isnumeric() == True:
c_1 = degree.f_2_c(bruh)
print("If the temperature is", bruh, "degrees in Fahrenheit, then it is", c_1 , "degrees in Celsius.")
更多回答
HI, thanks for the help, I have one question: If I already defined c_1 as float((5/9) * (float(bruh) - 32))
in f_2_c(bruh) in degree.py, why do I have to type c_1= degree.f_2_c(bruh)
in the other file?
嗨,谢谢你的帮助,我有一个问题:如果我已经定义了f2_c(Breh)中的c_1为Float((5/9)*(Float(Breh)-32)),为什么我必须在另一个文件中输入c_1=egree.f_2_c(Breh)?
" If I already defined c_1 as float((5/9) * (float(bruh) - 32)) in f_2_c(bruh) in degree.py" - first off, because that means, "take the value that bruh
has in this file, right now, compute a result, and let c_1
be a name for the result". Second, because that sets the value of c_1
for that module; every module gets its own set of variables, and if you want to use them from another module then you have to reach in to get them explicitly.
“如果我已经将f2_c(Breh)中的浮点数((5/9)*(Float(Breh)-32))定义为Float((5/9)*(Float(Breh)-32))”-First Off,因为这意味着“现在就取breh在这个文件中的值,计算一个结果,并让c_1作为结果的名称”。其次,因为它为该模块设置了c_1的值;每个模块都有自己的变量集,如果您想要从另一个模块使用它们,那么您必须进入以显式地获取它们。
When you define a function, the statements within the function are not executed. It is only executed when the function is called( c_1= degree.f_2_c(bruh) ) Documentation about functions: docs.python.org/3/tutorial/controlflow.html#defining-functions
定义函数时,不会执行该函数内的语句。仅当调用函数(c_1=度f_2_c(Breh))函数文档时才执行:docs.python.org/3/tutorial/controlflow.html#defining-functions
我是一名优秀的程序员,十分优秀!