2016-12-08 10 views
0

私は別のプログラムから関数をインポートする簡単なプログラムを書いています。基本的に華氏を摂氏に変換します。逆もまた同様です。ここでは、メインプログラムのコードは次のとおりです。私はそれを行うに行くとき、それは温度を取るよがありません1 tempに必要な位置引数

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) 

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

そして、ここでは、プログラムのコードは、インポートされる機能から来ているのですそれは華氏と摂氏の区別を受け入れます。しかし、それはこれを言うでしょう:

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

私は本当にこれを理解することはできませんので、任意の助けをいただければ幸いです。ありがとう。 main()

+0

'と:

celcius(32) 

と、あなたはどうなるあなたのプログラムの場合は0

になるだろう:試してみてくださいcelsius() 'の代わりに' temp'引数を使います。この引数は、両方の関数に必要です。 – elethan

答えて

1

あなたcelsiusにパラメータを渡すのを忘れてやfahrenheit機能。次のように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.') 
+0

ありがとう、私はそれを追加したように右に働いた:) –

1

あなたはtemp引数なしfahrenheit()celsius()の両方を呼び出していますが、位置引数tempを必要とするこれらの関数を定義します。

更新しますmain()機能次のように(また、条件付きのインポートを行う必要はありません。ただ、ファイルの先頭に両方の機能をインポートするには):

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

あなたの間違いは、温度のために引数を入力されていません。 `()`両方の `華氏(呼び出し)メイン`で

celcius(temp) 
関連する問題