2017-10-19 85 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

用'%s'替換'%d'' – 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

相關問題