2017-01-20 17 views
1

私はPythonには新しく、私は探検しています。次のコードでは、printステートメントから関数を呼び出そうとしていますが、合法ですか?print文からPythonの関数を呼び出せますか?

# My func 
def summer(num1,num2): 
print "The summation of the numbers is: %d " % (num1+num2) 

num1 = raw_input("Enter a number: ") 
num2 = raw_input("Enter another number: ") 

print "Again, the summation: %d " % (summer(num1,num2)) 

print "That is the end of the program." 

今ラインは

print "Again, the summation: %d " % (summer(num1,num2)) 

有効でしょうか?私はエラーが発生しています。
この方法で文字列から関数を呼び出すことはできますか?ありがとうございました。

私が手にエラーがある:あなたの方法でコードをインデントし、文字列を返す必要が

Traceback (most recent call last): 
    File "myfunc.py", line 9, in <module> 
    summer(num1,num2) 
    File "myfunc.py", line 3, in summer 
    print "The summation of the numbers is: %d " % (num1+num2) 
    TypeError: %d format: a number is required, not str 
+0

は「エラー」を取得、具体的に説明してくださいとあなたが得る正確なエラーを言及します。 –

+0

質問のエラーを編集しました。答えは@Eric Duminilによってすでに提供されています。 – Rio1210

答えて

2

。また、文字列を整数に変換する必要があります。

def summer(num1,num2): 
    return "The summation of the numbers is: %d " % (int(num1)+int(num2)) 

num1 = raw_input("Enter a number: ") 
num2 = raw_input("Enter another number: ") 

print "Again, the summation: %s " % (summer(num1,num2)) 

print "That is the end of the program." 
+0

ありがとう、これはこれを説明します。さらに、生の入力を取っているときに、データを格納/取り込むデフォルトのフォーマットは何ですか?文字列?私の質問は、** int ** ** int **(num1)に変換していることです** – Rio1210

+1

Raw_inputは、コンテンツが数字のように見えても常に文字列を返します。算術演算の場合、 "3"を3に変換する必要があります。 –

1

これは単純です。文字列を数値に変換するには、この操作を行います。あなたは

したがって、整数に変換したい

int(String) 

何でもした後に、あなたはどうなる:

num1 = raw_input("Enter a number: ") 
num1 = int(num1) 

が、より高速な方法は次のようになります。

num1 = int(raw_input("Enter a number: ")) 

num1を文字列として使用し、数値(i tはint(num1)の後の数値に変更されます

申し訳ありませんが、別の欠陥が気付いただけです。

数字が定義される前に合計を表示しなければならない理由が不思議でした。また、3行目と3行目から最後の行に%dが必要ないことを伝えたいと思っていました。

print "The summation of the numbers is", (num1+num2) 

別の傷を行う(編集#2)

は、最後のコメントはreturn "The summation of the numbers is: %d " % (num1+num2) にごprint "The summation of the numbers is: %d " % (num1+num2)を変えたが、人はあなたとその上に行きませんでした。 returnを実行するときは、戻り値が何であるかをコマンドの値にします。私に例を挙げてみましょう。

def MultNumByTwo(num): 
    num = num * 2 
    return num 
print MultNumByTwo(3) 

MultNumByTwoは、私は3を作って、それを新しい自体の値2倍を与えるnum = num * 2をしているnumをとるので、これは、6を出力します。今、私がreturn numをするとき、MultNumByTworeturnの後の数字の値を取る。

ここであなたのコードを見てみましょう。 print "The summation of the numbers is: %d " % (num1+num2)を実行すると、コマンドに値はありません。それは、これらの2つの数字を追加したと言われたことをちょうど実行します。したがって、コマンドになったので単独でしか使用できません。印刷コマンドでは使用できません。

% dはとにかくprintコマンドでコマンドを取るとしたら、それはAgain, the summation: %dだろうが、%dはThe summation of the numbers is: %d " % (num1+num2)と、コマンド全体になりますが

再び、合計を印刷します:数字の合計は、次のとおりです。( num1 + num2)

0

コードを少し修正しましたか? はちょうどinput()

def summer(num1,num2): 
    print "The summation of the numbers is: %d " % (num1+num2) 
    return num1+num2 

num1 = input("Enter a number: ") 
num2 = input("Enter another number: ") 

print "Again, the summation: %d " % (summer(num1,num2)) 
print "That is the end of the program." 

出力を使用することにより、NUM1とNUM2ためint値を取得することができます。

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
Enter a number: 5 
Enter another number: 10 
The summation of the numbers is: 15 
Again, the summation: 15 
That is the end of the program. 
>>> 
関連する問題