在函數check_price()
我試圖打開一個不存在的.txt文件來讀取和 我想用except語句來捕獲IOerror。除了語句
但之後,我創建了一個.txt文件,並且我想使用相同的函數來讀取它。但是,我的功能不讀取文件。誰能幫我?
我想做一個價格檢查程序。
樣品看起來像這樣。
Menu:
(I)nstructions
(L)oad Products
(S)ave Products
(A)dd Product
(C)heck Prices
(Q)uit
>>> C
No products.
Menu:
(I)nstructions
(L)oad Products
(S)ave Products
(A)dd Product
(C)heck Prices
(Q)uit
>>> L
Enter file name: products.txt
Menu:
(I)nstructions
(L)oad Products
(S)ave Products
(A)dd Product
(C)heck Prices
(Q)uit
>>> c
Huggies is $0.39 per unit.
Snugglers is $0.26 per unit.
Baby Love is $0.23 per unit.
我的功能:
def check_price():
while True:
try:
text_file = open("product.txt",'r')
break
except IOError:
print"no product"
text_file = open("product.txt",'r')
line = text_file.readline()
if line == "":
break
print ""
爲什麼'while True'?無法弄清楚。 – Tom