Apr-01-2017, 12:13 AM
So I'm taking a course, and I can't quite see what I'm doing wrong with this program.
[Image: mb2zobw.png]
Sorry, here's a more readable code block:
EDIT: don't worry, all solved
[Image: mb2zobw.png]
Moderator snippsat: Post code in code tag look at BBcode help
Sorry, here's a more readable code block:
fishy = "Call me Ishmael. Some years ago - never mind how long precisely - \ having little or no money in my purse, and nothing particular to interest me on \ shore, I thought I would sail about a little and see the watery part of the \ world. It is a way I have of driving off the spleen and regulating the \ circulation. Whenever I find myself growing grim about the mouth; whenever it \ is a damp, drizzly November in my soul; whenever I find myself involuntarily \ pausing before coffin warehouses, and bringing up the rear of every funeral I \ meet; and especially whenever my hypos get such an upper hand of me, that it \ requires a strong moral principle to prevent me from deliberately stepping into\ the street, and methodically knocking people's hats off - then, I account it \ high time to get to sea as soon as I can. This is my substitute for pistol and\ ball. With a philosophical flourish Cato throws himself upon his sword; I \ quietly take to the ship. There is nothing surprising in this. If they but \ knew it, almost all men in their degree, some time or other, cherish very \ nearly the same feelings towards the ocean with me." ishy = fishy.split() #in the first paragraph of moby dick, this function is designed to take an input #of a word, and at every point where it appears in the text, calculate the average #length of all the words that precede it every time it appears def prevword_ave_len(word): i = 0 curr_list = [] if ishy[0] == word: curr_list.append(len('')) return curr_list[0] elif word in ishy: for i in range(0,len(ishy)): if ishy[i] == word: curr_list.append(len(ishy[i-1])) i+=1 s = 0 for n in curr_list: s = s+n ave = '%.1f' % (s/len(curr_list)) return float(ave) else: return False print(prevword_ave_len('Call'))
EDIT: don't worry, all solved