2016-02-11 158 views
0

我正在學習Python。在做練習時,我得到錯誤並非在字符串格式化過程中轉換的所有參數

Python TypeError: not all arguments converted during string formatting

我已經搜索了答案,但我不明白我找到的代碼。

print "What's your name?", 
name = raw_input() 
print "What's your middle name?", 
middle_name = raw_input() 
print "Where do you live?", 
country = raw_input() 
print "Make of the car you drive?", 
car = raw_input() 
print "Are you single or married?", 
socstatus = raw_input() 
print "Are you Men or Woman?", 
sex = raw_input() 

print "Let me see if I understood correctly. Your complete name is %r %r, you live in %r, rides on a %r, and you are a %r $r." % (name, middle_name, country, car, socstatus, sex) 
+0

你沒告訴我們你在說什麼錯誤 –

+1

你最後的print語句用'$ r',而不是一個'%r' – yurib

+0

結束我承認,代碼;你忽略了「酷」這個詞。 :) – zondo

回答

0

這是一個簡單的語法問題:你的最後的格式規範似乎是$ R代替%R。正因爲如此,你只有五種格式規格,但六個變量。改變最後一個。

print "Let me see if I understood correctly. Your complete name is %r %r, 
you live in %r, rides on a %r, and you are a %r %r." % (name, middle_name, 
country, car, socstatus, sex) 
0

您使用$ r而不是%r作爲最終變量。

0

您在格式字符串中有$r,而不是%r。但是,請記住,%r格式樣式已被棄用。你應該使用str.format(),它當前支持位置參數。

# Get variables... 
print("Let me see if I understood correctly. Your complete name is {} {}, you live in {}, rides on a {}, and you are a {} {}.".format(name, middle_name, country, car, socstatus, sex)) 
相關問題