Python temp缺少1个必需的位置参数
我正在编写一个简单的程序,从另一个程序导入函数。它基本上把华氏温度转换成摄氏温度,反之亦然,这取决于你给它什么样的输入。以下是主程序的代码:Python temp缺少1个必需的位置参数,python,python-3.x,arguments,Python,Python 3.x,Arguments,我正在编写一个简单的程序,从另一个程序导入函数。它基本上把华氏温度转换成摄氏温度,反之亦然,这取决于你给它什么样的输入。以下是主程序的代码: def main(): temp = int(input('What is the temperature? ')) print('Is this temperature in fahrenheit or celsius?') system = int(input('Please put 1 for Fahrenheit and 2
def main():
temp = int(input('What is the temperature? '))
print('Is this temperature in fahrenheit or celsius?')
system = int(input('Please put 1 for Fahrenheit and 2 for Celsius: '))
if system == 1:
from tempconvert import celsius
celsius()
elif system == 2:
from tempconvert import fahrenheit
fahrenheit()
else:
print('I dont understand.')
main()
下面是程序的代码,正在导入的函数来自:
def fahrenheit(temp):
fahrenheit = temp * 1.8 + 32
print('Your temperature in fahrenheit is ', fahrenheit)
def celsius(temp):
celcius = temp - 32
celsius = celcius / 1.8
print('Your temperature in celsius is ', celsius)
当我去做的时候,它会接受我输入的温度,它会接受华氏温度和摄氏温度的区别。但它会说:
celsius() missing 1 required positional argument: 'temp'
我真的搞不懂,所以任何帮助都将不胜感激。谢谢。在main()
中调用fahrenheit()
和cellices()
,而不使用temp
参数,但您将这些函数定义为需要位置参数temp
按如下方式更新main()
函数(另外,不需要执行条件导入;只需在文件顶部导入两个函数即可):
在main()
按如下方式更新main()
函数(另外,不需要执行条件导入;只需在文件顶部导入两个函数即可):
您忘记将参数传递给摄氏
和华氏
函数。更新main()
函数,如下所示:
def main():
temp = int(input('What is the temperature? '))
print('Is this temperature in fahrenheit or celsius?')
system = int(input('Please put 1 for Fahrenheit and 2 for Celsius: '))
if system == 1:
from tempconvert import celsius
celsius(temp) # pass 'temp' as parameter
elif system == 2:
from tempconvert import fahrenheit
fahrenheit(temp) # pass 'temp' as parameter
else:
print('I dont understand.')
您忘记将参数传递给摄氏
和华氏
函数。更新main()
函数,如下所示:
def main():
temp = int(input('What is the temperature? '))
print('Is this temperature in fahrenheit or celsius?')
system = int(input('Please put 1 for Fahrenheit and 2 for Celsius: '))
if system == 1:
from tempconvert import celsius
celsius(temp) # pass 'temp' as parameter
elif system == 2:
from tempconvert import fahrenheit
fahrenheit(temp) # pass 'temp' as parameter
else:
print('I dont understand.')
你的错误是没有加入温度的论证。尝试:
celcius(32)
你会得到0
对于您的程序,您将执行以下操作:
celcius(temp)
你的错误是没有加入温度的论证。尝试:
celcius(32)
你会得到0
对于您的程序,您将执行以下操作:
celcius(temp)
在main()。这两个函数都需要此参数。在main()
中,您可以调用fahrenheit()
和cellices()
,而不使用temp
参数。这两个函数都需要此参数。