Re: [問題] 一個本文處理的練習

看板Python作者 (suzuke)時間14年前 (2011/08/25 17:03), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串2/2 (看更多)
我猜你用的python版本應該是3.x吧? 忘記那一版開始 print已經被改成函式print()了 改成這樣應該就可以了 total = 0 print("%10s %10s %10s " % ('Names', 'Shares', 'Price')) print("---------- " * 3); for line in open('portfolio.txt', 'r'): vals = line.split() symbol = vals[0] shares = int(vals[1]) price = float(vals[2]) print("%10s %10s %10.2f" % (symbol, shares, price)) total += shares * price print("\nTotal value : $%0.2f" % total) ※ 引述《Jason1122 (Jason1122)》之銘言: : 在網路上的blog看到一個練習 : 要處理這份文件:http://www.givegoodweb.com/examples/portfolio.txt : 程式碼 : total = 0 : print "%10s %10s %10s " % ('Names', 'Shares', 'Price') : print "---------- " * 3; : for line in open('portfolio.txt', 'r'): : vals = line.split() : symbol = vals[0] : shares = int(vals[1]) : price = float(vals[2]) : print "%10s %10s %10.2f" % (symbol, shares, price) : total += shares * price : print "\nTotal value : $%0.2f" % total : 我照著打一遍,覺得很合理,但只要執行都會說最後一行 : print "\nTotal value : $%0.2f" % total,有問題 : 出現SyntaxError: invalid syntax : 存成text檔去執行,執行後畫面馬上消失,想說把最後一行去掉好了,一樣馬上消失 : ,但把最後一行去後,在eric4上面是可以執行的,可是在shell裡面有沒有最後一行, : 都沒有反應,也不會出現錯誤訊息,怎麼在三種地方會有三種不同解果, : 該怎樣才能用text執行連最後一行都跑出來,而且不會馬上關閉 : 煩請協助幫忙,謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.194.200.175

08/26 09:46, , 1F
不是耶,我是用Python 2.7 ~"~
08/26 09:46, 1F

08/26 11:50, , 2F
那我就不知道了= =a
08/26 11:50, 2F

08/26 11:50, , 3F
我試2.6 2.7都正常(你的code)
08/26 11:50, 3F

08/26 11:56, , 4F
樓上 原po現在可以跑了 馬上消失+ raw_input()也搞定了
08/26 11:56, 4F

08/26 11:56, , 5F
可是他右鍵 open with python 竟然沒用...也是馬上關掉
08/26 11:56, 5F

08/26 13:46, , 6F
在cmd裡面執行就不會馬上關掉
08/26 13:46, 6F

08/26 17:28, , 7F
感謝各位熱心人士 問題解決了+ raw_input()就可以了
08/26 17:28, 7F
文章代碼(AID): #1ELe1CjN (Python)
討論串 (同標題文章)
文章代碼(AID): #1ELe1CjN (Python)