我現在有 -將文件列入打印最便宜的價格。蟒
showFile = open("products.txt", 'r')
lines = showFile.readline()
while lines:
print(lines)
lines = showFile.readline()
showFile.close()
showFile = open("products.txt", 'r')
numbers = []
for line in showFile:
tokens = line.split(',')
numbers.append(min(t for t in tokens[0]))
minValue = min(numbers)
print(minValue)
Products.txt -
'Snicker's, 33, 84
'Mars', 18.5, 72
'Bounty', 22, 96
所以其中33是價格,和84是數量。 和18.5是價格,72是數量等。
即時通訊試圖讓它打印一些像 士力架每單位0.39美元。 火星每單位0.29美元。 賞金是每單位0.23美元。 賞金是最便宜的
幫助表示讚賞:d
開始除以一個數由其他,然後瞭解字符串格式化語法:http://docs.python.org/2/library/string.html#format -string-syntax – Patashu 2013-05-09 23:06:17
你不需要(或者想)兩次打開同一個文件,只需要(1)打印到屏幕然後(2)追加到列表中。使用相同的迭代器(實際上,使用'with'上下文)並同時完成這兩個任務。 – hexparrot 2013-05-09 23:31:23