2013-03-28 71 views
0
guessesRemaining=12 
Summary=[] 


code=['1','2','3','4'] 



while guessesRemaining > 0: 
report=[] 
guess=validateInput() 
guessesRemaining -= 1 
if guess[0] == code[0]: 
    report.append("X") 
if guess[1] == code[1]: 
    report.append("X") 
if guess[2] == code[2]: 
    report.append("X") 
if guess[3] == code[3]: 
    report.append("X") 

tempCode=list(code) 
tempGuess=list(guess) 

if tempGuess[0] in tempCode: 
    report.append("O") 
if tempGuess[1] in tempCode: 
    report.append("O") 
if tempGuess[2] in tempCode: 
    report.append("O") 
if tempGuess[3] in tempCode: 
    report.append("O") 

ListCount=report.count("X") 
if ListCount > 0: 
    del report[-ListCount:] 

report2=report[0:4] 
dash=["-","-","-","-"] 
report2=report+dash 
report3=report2[0:4] 
report4="".join(report3) 
guess2="".join(guess) 
Summary+=[guess2,report4] 

print(Summary) 

validateInput()調用一個我沒有在這裏添加的函數。我試圖弄清楚如何在整個12次猜測的過程中一次打印一行結果。通過猜三次我收到...如何逐行打印配對元素?

['4715', 'OO--', '8457', 'O---', '4658', 'O---'] 

時,我想收到...

['4715', 'OO--'] 
['8457', 'O---'] 
['4658', 'O---'] 

我一直在努力,在\ n以多種方式補充,但我無法弄清楚如何執行它。任何和所有的幫助,非常感謝。

+0

請修復您的縮進 – jamylak 2013-03-28 03:45:30

+0

這是爲遊戲「主謀」,對不對?我可以對邏輯提出很多簡化,但這不是一個代碼審查網站...... – 2013-03-28 04:28:52

回答

0

,我認爲你需要像

In [1]: l = ['4715', 'OO--', '8457', 'O---', '4658', 'O---'] 

In [2]: l1 = l[::2] # makes a list ['4715', '8457', '4658'] 

In [3]: l2 = l[1::2] # makes ['OO--', 'O---', 'O---'] 

In [4]: for i in zip(l1, l2): 
    ...:  print i 
    ...: 
('4715', 'OO--') 
('8457', 'O---') 
('4658', 'O---') 
1

我試着用多種方法添加\ n,但我無法弄清楚如何實現它。

如果您在第一時間正確地構造數據,它將有很大幫助。

Summary+=[guess2,report4] 

這意味着 「追加在[guess2,report4]發現每個項目,分別以Summary」。

看來你的意思是「把[guess2,report4]作爲一個單獨的項目追加到Summary」。要做到這一點,你需要使用列表的append方法:

Summary.append([guess2, report4]) 

現在,我們有對,每一個我們要顯示在單獨的行列表,它會容易得多:

for pair in Summary: 
    print(pair)