打印存储函数内部字符串的变量 - Python

时间:2017-10-19 17:56:39

标签: 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() 吗?

{{1}}

2 个答案:

答案 0 :(得分:3)

%d用于数字。请改用%s

答案 1 :(得分:0)

如果您尝试使用%d打印字符串,则会显示错误信息。

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

所以尝试用%s替换那些%d。 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