需要帮助从python中的另一个类调用函数吗
我写了一个简单的单位转换程序。在main()类中,我有一个基本程序,要求用户选择要使用的转换器。然后我为每个转换器编写了一个特定的类。我想做的是将每个转换器方法导入main。说实话,我不确定我是否需要main()。无论如何,所有的类都可以工作,我只是不知道如何将这些方法导入到它们相应的占位符中。我希望这是有道理的需要帮助从python中的另一个类调用函数吗,python,function,class,methods,import,Python,Function,Class,Methods,Import,我写了一个简单的单位转换程序。在main()类中,我有一个基本程序,要求用户选择要使用的转换器。然后我为每个转换器编写了一个特定的类。我想做的是将每个转换器方法导入main。说实话,我不确定我是否需要main()。无论如何,所有的类都可以工作,我只是不知道如何将这些方法导入到它们相应的占位符中。我希望这是有道理的 print("Welcome to the Amazing Converter!\n") def main(): print("[1] Con
print("Welcome to the Amazing Converter!\n")
def main():
print("[1] Convert Temperature")
print("[2] Convert Distance")
print("[3] Convert Weight")
again = True
while True:
try:
choice = int(input("What would you like to convert? "))
except ValueError:
print("Invalid entry.")
if choice == 1:
print("[1] Convert from Celsius to Farenheit")
print("[2] Convert from Farenheit to Celsius")
temp_choice = int(input("Enter your choice: "))
if temp_choice == 1:
pass
elif choice == 2:
pass
elif choice == 2:
print("[1] Convert from Miles to Kilometers")
print("[2] Convert from Kilometers to Miles")
dist_choice = int(input("Enter your choice: "))
if dist_choice == 1:
pass
elif dist_choice == 2:
pass
elif choice == 3:
print("[1] Convert from Pounds to Kilos")
print("[2] Convert from Kilos to Pounds")
weight_choice = int(input("Enter your choice: "))
if weight_choice == 1:
pass
elif weight_choice == 2:
pass
again = input("Would you like to Convert another unit of measurement? ")
if again == 1:
True
else:
print("Have a good day!")
break
if __name__ == "__main__":
main()
#Convert Celsius to Farenheit
class Temperature:
temp = 0
def __init__(self, temp):
self.temp = temp
def cel_to_far(self):
res = (self.temp * 9/5) + 32
return res
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
#Convert Farenheit to Celsius
class Temperature:
temp = 0
def __init__(self, temp):
self.temp = temp
def far_to_cel(self):
res = (self.temp - 32) * 5/9
return res
farenheit = float(input("Enter farenheit value: "))
cel_temp = Temperature(farenheit)
print(round(cel_temp.far_to_cel()))
#Convert miles to kilometers
class Distance:
dist = 0
def __init__(self, dist):
self.dist = dist
def mil_to_kil(self):
res = (self.dist * 1.609344)
return res
miles = float(input("Enter mile value: "))
miles_dist = Distance(miles)
print(round(miles_dist.mil_to_kil()))
#Convert kilometers to miles
class Distance:
dist = 0
def __init__(self, dist):
self.dist = dist
def kil_to_mil(self):
res = (self.dist * 0.62137)
return res
kilometers = float(input("Enter kilometer value: "))
kilo_dist = Distance(kilometers)
print(round(kilo_dist.kil_to_mil()))
#Convert pounds to kilos
class Weight:
def __init__(self, weight):
self.weight = weight
def pound_to_kilo(self):
res = (self.weight * 0.45359237)
return res
pounds = float(input("Enter pound value: "))
pound_weight = Weight(pounds)
print(round(pound_weight.pound_to_kilo()))
#Convert kilos to pounds
class Weight:
def __init__(self, weight):
self.weight = weight
def kilo_to_pound(self):
res = (self.weight / 0.45359237)
return res
kilos = float(input("Enter kilo value: "))
kilo_weight = Weight(kilos)
print(round(kilo_weight.kilo_to_pound()))
您已经在每个类下面有了如何使用该类的示例。例如,在下面定义了
温度类的地方,您有:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
if temp_choice == 1:
pass
您需要做的是将它们移动到main
的相关部分,在那里您可以使用它们。例如,您有:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
if temp_choice == 1:
pass
您可以将其(适当缩进)放置在通行证的位置
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
但是,您需要做的另一件事是移动调用main
的代码,即:
if __name__ == "__main__":
main()
在定义了其他类之后,返回到模块末尾
如果您现在就这样尝试,您将得到一个namererror
,因为在调用main
时还没有定义其他类
事实上,你并不真的需要
if __name__ == "__main__":
除非您还可以将模块导入另一个模块。如果您知道只将其作为主程序直接运行(python your_module.py
),那么只需将main()
放在末尾就足够了
最后,用于转换的类(例如,两个方向上的温度)似乎具有相同的名称(均称为温度
)。虽然您可以重命名它们以避免名称冲突,但实际上它们的形式适合合并到一个类中,该类既有cel\u-to\u-far
方法,也有far\u-to\u-cel
方法,同样适用于其他类
将所有这些放在一起(并删除关于重复“是否要转换另一个单元?”的部分,这超出了本问题的范围),代码如下所示:
print("Welcome to the Amazing Converter!\n")
def main():
print("[1] Convert Temperature")
print("[2] Convert Distance")
print("[3] Convert Weight")
again = True
while True:
try:
choice = int(input("What would you like to convert? "))
except ValueError:
print("Invalid entry.")
if choice == 1:
print("[1] Convert from Celsius to Farenheit")
print("[2] Convert from Farenheit to Celsius")
temp_choice = int(input("Enter your choice: "))
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
elif temp_choice == 2:
farenheit = float(input("Enter farenheit value: "))
cel_temp = Temperature(farenheit)
print(round(cel_temp.far_to_cel()))
elif choice == 2:
print("[1] Convert from Miles to Kilometers")
print("[2] Convert from Kilometers to Miles")
dist_choice = int(input("Enter your choice: "))
if dist_choice == 1:
miles = float(input("Enter mile value: "))
miles_dist = Distance(miles)
print(round(miles_dist.mil_to_kil()))
elif dist_choice == 2:
kilometers = float(input("Enter kilometer value: "))
kilo_dist = Distance(kilometers)
print(round(kilo_dist.kil_to_mil()))
elif choice == 3:
print("[1] Convert from Pounds to Kilos")
print("[2] Convert from Kilos to Pounds")
weight_choice = int(input("Enter your choice: "))
if weight_choice == 1:
pounds = float(input("Enter pound value: "))
pound_weight = Weight(pounds)
print(round(pound_weight.pound_to_kilo()))
elif weight_choice == 2:
kilos = float(input("Enter kilo value: "))
kilo_weight = Weight(kilos)
print(round(kilo_weight.kilo_to_pound()))
class Temperature:
temp = 0
def __init__(self, temp):
self.temp = temp
def cel_to_far(self):
res = (self.temp * 9/5) + 32
return res
def far_to_cel(self):
res = (self.temp - 32) * 5/9
return res
class Distance:
dist = 0
def __init__(self, dist):
self.dist = dist
def mil_to_kil(self):
res = (self.dist * 1.609344)
return res
def kil_to_mil(self):
res = (self.dist * 0.62137)
return res
class Weight:
def __init__(self, weight):
self.weight = weight
def pound_to_kilo(self):
res = (self.weight * 0.45359237)
return res
def kilo_to_pound(self):
res = (self.weight / 0.45359237)
return res
main()
您已经在每个类下面有了如何使用该类的示例。例如,在下面定义了温度类的地方,您有:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
if temp_choice == 1:
pass
您需要做的是将它们移动到main
的相关部分,在那里您可以使用它们。例如,您有:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
if temp_choice == 1:
pass
您可以将其(适当缩进)放置在通行证的位置
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
但是,您需要做的另一件事是移动调用main
的代码,即:
if __name__ == "__main__":
main()
在定义了其他类之后,返回到模块末尾
如果您现在就这样尝试,您将得到一个namererror
,因为在调用main
时还没有定义其他类
事实上,你并不真的需要
if __name__ == "__main__":
除非您还可以将模块导入另一个模块。如果您知道只将其作为主程序直接运行(python your_module.py
),那么只需将main()
放在末尾就足够了
最后,用于转换的类(例如,两个方向上的温度)似乎具有相同的名称(均称为温度
)。虽然您可以重命名它们以避免名称冲突,但实际上它们的形式适合合并到一个类中,该类既有cel\u-to\u-far
方法,也有far\u-to\u-cel
方法,同样适用于其他类
将所有这些放在一起(并删除关于重复“是否要转换另一个单元?”的部分,这超出了本问题的范围),代码如下所示:
print("Welcome to the Amazing Converter!\n")
def main():
print("[1] Convert Temperature")
print("[2] Convert Distance")
print("[3] Convert Weight")
again = True
while True:
try:
choice = int(input("What would you like to convert? "))
except ValueError:
print("Invalid entry.")
if choice == 1:
print("[1] Convert from Celsius to Farenheit")
print("[2] Convert from Farenheit to Celsius")
temp_choice = int(input("Enter your choice: "))
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
far_temp = Temperature(celsius)
print(round(far_temp.cel_to_far()))
elif temp_choice == 2:
farenheit = float(input("Enter farenheit value: "))
cel_temp = Temperature(farenheit)
print(round(cel_temp.far_to_cel()))
elif choice == 2:
print("[1] Convert from Miles to Kilometers")
print("[2] Convert from Kilometers to Miles")
dist_choice = int(input("Enter your choice: "))
if dist_choice == 1:
miles = float(input("Enter mile value: "))
miles_dist = Distance(miles)
print(round(miles_dist.mil_to_kil()))
elif dist_choice == 2:
kilometers = float(input("Enter kilometer value: "))
kilo_dist = Distance(kilometers)
print(round(kilo_dist.kil_to_mil()))
elif choice == 3:
print("[1] Convert from Pounds to Kilos")
print("[2] Convert from Kilos to Pounds")
weight_choice = int(input("Enter your choice: "))
if weight_choice == 1:
pounds = float(input("Enter pound value: "))
pound_weight = Weight(pounds)
print(round(pound_weight.pound_to_kilo()))
elif weight_choice == 2:
kilos = float(input("Enter kilo value: "))
kilo_weight = Weight(kilos)
print(round(kilo_weight.kilo_to_pound()))
class Temperature:
temp = 0
def __init__(self, temp):
self.temp = temp
def cel_to_far(self):
res = (self.temp * 9/5) + 32
return res
def far_to_cel(self):
res = (self.temp - 32) * 5/9
return res
class Distance:
dist = 0
def __init__(self, dist):
self.dist = dist
def mil_to_kil(self):
res = (self.dist * 1.609344)
return res
def kil_to_mil(self):
res = (self.dist * 0.62137)
return res
class Weight:
def __init__(self, weight):
self.weight = weight
def pound_to_kilo(self):
res = (self.weight * 0.45359237)
return res
def kilo_to_pound(self):
res = (self.weight / 0.45359237)
return res
main()
您创建了重复的类。所以,让他们更容易,就像
class Temperature:
def cel_to_far(self, temp):
res = (temp * 9/5) + 32
return res
def far_to_cel(self, temp):
res = (temp - 32) * 5/9
return res
并为该类创建对象,如下所示
def main():
# creating object
temperature = Temperature()
把温度方法称为
if choice == 1:
print("[1] Convert from Celsius to Farenheit")
print("[2] Convert from Farenheit to Celsius")
temp_choice = int(input("Enter your choice: "))
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
print(round(temperature.cel_to_far(celsius)))
elif choice == 2:
farenheit = float(input("Enter farenheit value: "))
print(round(temperature.far_to_cel(farenheit)))
希望你能得到它。你创建了重复的类。所以,让他们更容易,就像
class Temperature:
def cel_to_far(self, temp):
res = (temp * 9/5) + 32
return res
def far_to_cel(self, temp):
res = (temp - 32) * 5/9
return res
并为该类创建对象,如下所示
def main():
# creating object
temperature = Temperature()
把温度方法称为
if choice == 1:
print("[1] Convert from Celsius to Farenheit")
print("[2] Convert from Farenheit to Celsius")
temp_choice = int(input("Enter your choice: "))
if temp_choice == 1:
celsius = float(input("Enter celsius value: "))
print(round(temperature.cel_to_far(celsius)))
elif choice == 2:
farenheit = float(input("Enter farenheit value: "))
print(round(temperature.far_to_cel(farenheit)))
希望您能理解。注意:刚才看到一些类具有相同的名称。即将处理这件事。。。注意这个空间。阿兰尼维,非常感谢你的帮助!我已经用main()中的方法中的PASS替换了类中的代码。事实上,我已经完成了您建议的所有操作(除了对每个类使用两个方法,而不是使用两个类(duh!)),但最后我忘了关闭main()。您提到不需要if name==“main”:除非我要从另一个文件导入类。这样我就可以在一个文件中只保留main(),而在另一个文件中保留其他类。如果你能教我怎么做,我将不胜感激。再次感谢你的帮助@当然,您可以将这些类移动到另一个文件中(比如说myclasses.py
),然后您在这里所要做的就是从myclasses导入温度、距离、重量
。如果您这样做了,那么这个模块只包含main
函数,那么您想将这个模块导入另一个模块的可能性就更小了,因此更不用担心的问题了,如果在调用底部的main()
之前进行测试。好的!我会尽力让你知道的。多谢各位!注意:刚才看到一些类具有相同的名称。即将处理这件事。。。注意这个空间。阿兰尼维,非常感谢你的帮助!我已经用main()中的方法中的PASS替换了类中的代码。事实上,我已经完成了您建议的所有操作(除了对每个类使用两个方法,而不是使用两个类(duh!)),但最后我忘了关闭main()。您提到不需要if name==“main”:除非我要从另一个文件导入类。这样我就可以在一个文件中只保留main(),而在另一个文件中保留其他类。如果你愿意,我将不胜感激