[問題] print畫面資料 寫出一個檔

作者: newdas (.)   2018-12-08 23:01:37
HI all
最近在修課時發現常常需要多次反覆執行計算
所以在思考 有沒有辦法讓其 設定
1.設定重複執行幾次
2.結果將每次結果寫出 (已解決:)
還請大大可以指點 謝謝大家
=======================================================
def Queens():
status =
[0,1,2,3,4,5,6,7] #初始皇后都在對角線
#當存在衝突個數大於0時,循環球解最佳後繼,直到找到最後一個皇后
while get_numof_conflict(status) > 0:
status = hill_climbing(status)
print status
print get_numof_conflict(status)
f = open("twice.txt", "a")
sys.stdout = f
print "the answer is"
print
print status
time.sleep(2)
tEnd = time.time()#計時結束
print "It cost %f sec" % (tEnd - tStart)#會自動做近位
#列印結果
file = open('data.txt','w+')
# print>>file
# file.close()
===========================================================
作者: swampni   2018-12-09 10:34:00
試試看file.write()

Links booklink

Contact Us: admin [ a t ] ucptt.com