[英]Print variables storing a strings inside a function - Python
我向該函數添加了一個參數,以及多個參數。 我也將變量移到函數之外,每次都會出現錯誤。 需要str()
嗎?
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()
%d
用於數字。 請改用%s
。
如果嘗試使用%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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.