我正在输入一个示例代码。我将温度定义为主要函数,但无法运行它。它自动关闭,没有任何输入提示。你知道吗
def temperature():
number = input('Enter what you want\n')
values = number.split(' ')
temperature = values[0]
unitin = values[1]
unitout = values[-1]
print(temperature, 'this', unitin, 'is', unitout, 'working') # this is working is a test statemment i was unsure
print('This is a test function', number)
def main():
temperature()
if __name__ == "__main__":
main()
这是运行的部分代码。但当我试图改变主要功能的名称时,它就停止了工作。我不确定,它只取“main”这个名字吗?你知道吗
def readinput():
input_string = input('Enter what you want\n')
values = input_string.split(' ')
temperature = values[0]
unitin = values[1]
unitout = values[-1]
print(temperature, 'this', unitin, 'is', unitout, 'working')
print('This is a test function', input_string)
def temperature_converter():
readinput()
if __name__ == "__temperature_converter__":
temperature_converter()
这是不起作用的代码。谢谢您。你知道吗
你混淆了特殊变量
__name__
的内容,该变量包含字符串'__main__'
,以防模块自身运行(而不是由另一个模块导入)和函数main()
这些都是完全不相关的东西,当你将函数main()
的名称更改为你喜欢的其他名称时,保持字符串__main__
不变。你知道吗或者您可以完全删除以
if __name__ ==
开头的行,它也应该在没有is的情况下工作。你知道吗如果您自己运行代码,那么解释器会自动将变量
__name__
设置为"__main__"
。那和你的名字没有关系。看看What does if __name__ == "__main__": do?这应该起作用:
相关问题 更多 >
编程相关推荐