2017-10-19 8 views
0

私は、関数に複数のパラメータだけでなく、単一のパラメータを追加しました。私も関数の外に変数を移動し、毎回エラーが発生します。 str()が必要ですか?関数内に文字列を格納する変数を出力する - Python

def g_chord(): 
    string_1 = "G note" 
    string_2 = "B note" 
    string_3 = "D note" 
    print "A 'G' chord consists of the 1st, 3rd, and 5th notes in the G scale. Those notes are a, %d, %d, and %d." % (string_1, string_2, string_3) 

g_chord() 
+2

「%d」を「%s」に置き換えます – MaximTitarenko

答えて

3

%dが数字に使用されます。代わりに%sを使用してください。

0

%dで文字列を印刷しようとすると、あなたとエラーが発生します。

Traceback (most recent call last): 
File "<stdin>", line 5, in <module> 
TypeError: %d format: a number is required, not str 

だから%dを%sに置き換えてみてください。 print "A 'G' chord consists of the 1st, 3rd, and 5th notes in the G scale. Those notes are a, %d, %d, and %d." % (string_1, string_2, string_3

関連する問題