2013-03-23 49 views
0
mconfigQuantity = int(raw_input('Enter the number of (m)achine-configurations that you will need: ')) 
numberOfmconfig = 1 


for mconfigs in range(1,mconfigQuantity+1): 
    # %s conditional for mcidentifier 
    STRINGnumberOfmconfig = str(numberOfmconfig) 
    if STRINGnumberOfmconfig[-1] == '1': 
     suffix = 'st' 
    elif STRINGnumberOfmconfig[-1] == '2': 
     suffix = 'nd' 
    elif STRINGnumberOfmconfig[-1] == '3': 
     suffix = 'rd' 
    else: suffix = 'th' 
    finalConfigName = STRINGnumberOfmconfig + suffix 
    mcidentifier = raw_input('Enter the letter of your %s (m)achine-configuration: ') % (finalConfigName) 
    numberOfmconfig = numberOfmconfig + 1 






print mconfig() 

我已經寫了幾次,並重寫了這段代碼,對於我爲什麼打印'%s'而不是用我爲它指定的數據替換它沒有任何意義。我無法找到任何表明它不工作的原因。爲什麼我的%s格式說明符在這段代碼中沒有捕獲?

回答

5

應該

mcidentifier = raw_input(
    'Enter the letter of your %s (m)achine-configuration: ' % finalConfigName) 

您的%操作之前,所以它被應用到生成的字符串,即由raw_input返回一個封閉的括號。

另外請注意,在新代碼的新字符串格式化語法建議:

mcidentifier = raw_input(
'Enter the letter of your {} (m)achine-configuration: '.format(finalConfigName)) 
相關問題