1
我給包含許多行像下面......許多隨機信息的Python:通過
Spiaks餐廳的文本文件列表遍歷| 42.74 | -73.70 | 2阿奇博爾德ST +華特甫,NY 12189 | http://www.yelp.com/biz/spiaks-restaurant-watervliet|Italian|4|5|4|3|3|4|4
例如,Spiaks餐廳是在位置0,42.74是在位置1,-73.70處於位置2 ....意大利處於位置5 ... 4 | 5 | 4 | 3 | 3 | 4 | 4是另一列表...所以基本上列表內的列表,以及數字4是在6位,5在7位..等
我必須詢問用戶,並且用戶應該回復:
What type of restaurant would you like => Italian
What is the minimum rating => 3.6
結果應該是:
Name: Spiaks Restaurant; Rating 3.86
Name: Lo Portos; Rating 4.00
Name: Verdiles Restaurant; Rating 4.00
Found 3 restaurants.
這裏是我的代碼:
rest_type = raw_input("What type of restaurant would you like => ")
min_rate = float(raw_input("What is the minimum rating => "))
def parse_line(text_file):
count = 0.0
a_strip = text_file.strip('\n')
b_split = a_strip.split('|')
for i in range(6, len(b_split)):
b_split[i] = int(b_split[i]) # Takes the current indices in the list and converts it to integers
count += b_split[i] # Add up all of the integers values to get the total ratings
avg_rate = count/len(b_split[6:len(b_split)]) # Takes the total ratings & divides it by the length
#The above basically calculates the average of the numbers like 4|5|4|3|3|4|4
if (rest_type == b_split[5] and avg_rate >= min_rate):
print b_split[0], avg_rate
與結果的問題..我得到:
None
我知道這是一個很長很長的問題,但如果有人能給我一些見解,我將不勝感激!