0
votes

I'm writing a simple program that imports functions from another program. It basically converts Fahrenheit to Celsius or vice versa, depending on what kind of input you give it. Here's the code for the main program:

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()

And here's the code for the program the functions being imported are coming from:

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)

When I go do it, it'll take the temperature I enter, and it'll accept the distinction between Fahrenheit and Celsius. But then it'll say this:

celsius() missing 1 required positional argument: 'temp'

I really cannot figure this out, so any help would be appreciated. Thanks.

3
In main() you call both fahrenheit() and celsius() without the temp argument. This argument is required for both functions. - elethan

3 Answers

1
votes

Your forgot to pass the parameter to celsius and fahrenheit function. Update your main() function as follows:

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.')
1
votes

In main() you call both fahrenheit() and celsius() without the temp argument, but you define these functions as requiring a positional argument temp.

Update your main() function as follows (also, there is no need to do conditional importing; just import both functions at the top of the file):

from tempconvert import fahrenheit, celsius


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:
        celsius(temp)
    elif system == 2:
        fahrenheit(temp)
    else:
        print('I dont understand.')
0
votes

Your mistake is not entering the argument in for the temperature. Try:

celcius(32)

and you would get 0

In the case of your program you would do:

celcius(temp)