Python Forum
Can't figure out small errors in my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't figure out small errors in my code?
#1
Problem 2
You are working in a biology lab where they are investigating the effects of diet on
1
rats’ body weight. They have fed several rats various diets and stored their data in a
spreadsheet table like this:
rat name diet weight (g)
Whiskers rat chow 300.0
Mr. Squeeky swiss cheese 450.0
Pinky rat chow 320.0
Fluffball swiss cheese 500.0
The spreadsheet software exports this data to Python in the form of a list of tuples, where
each tuple is a row from the spreadsheet:
# signature: list (tuple (str , str , float))
data = \
[
('Whiskers' , 'rat chow' , 300.0) ,
('Mr. Squeeky' , 'swiss cheese' , 450.0) ,
('Pinky' , 'rat chow' , 320.0) ,
('Fluffball' , 'swiss cheese' , 500.0)
]
Your task is to write a Python function to calculate the average (mean) weight of the
rats on a given diet. The function should be called “avg_weight” and should take two
arguments: first a string representing a diet, and second a list of tuples (as shown above)
containing the data exported from the spreadsheet. It should return the average weight
of the rats on the given diet. If there are no rats on the given diet, your function should
return 0.0.
def avg_weight (diet , data) :
"""
signature: str , list (tuple (str , str , float) -> float
returns the average weight of the rats on the given diet,
or 0.0 if no rats match
"""
pass # TODO: implement the body of this function
Your function should behave as follows on the data shown above:
>>> avg_weight ('rat chow' , data)
310.0
>>> avg_weight ('swiss cheese ' , data)
475.0
>>> avg_weight ('quinoa ' , data)
0.0
Hint: iterate over the list of tuples and append the weight from each one that matches the
given diet to an accumulator list. Then return either 0.0 or the average of the numbers
in the accumulator list, depending on whether or not the accumulator list is empty. The
built-in sum function may be useful for calculating the average of the numbers in the list.

Problem 3
The position of a word in a string is the index of that word in list of words comprising
the string. For example:
It
0
was
1
the
2
best
3
of
4
times
5
it
6
was
7
the
8
worst
9
of
10
times
11
2
Write a function called “word_positions” with signature str -> dict (str -> list (int)).
It should return a dictionary whose keys are the words that occur in the argument string,
and where the value associated to a given key is the list of positions at which that word
occurs.
For example:
>>> word_positions ('It was the best of times it was the worst of times ')
{'It': [0], 'was ': [1, 7], 'the ': [2, 8], 'best ': [3], 'of': [4, 10],
'times ': [5, 11], 'it': [6], 'worst ': [9]}
Hint: for the purposes of this problem you may assume that the string does not include
any punctuation. Also, you do not need to do any case normalization, so in this problem
'It' and 'it' are considered different words. To obtain the list of words in a string, call
the str.split function/method:
>>> str.split ('welcome to the future ')
['welcome ', 'to', 'the', 'future ']
>>> "now's the time".split ()
["now's", 'the', 'time ']

MY CODES PROBLEMS 3 and 4:
def avg_weight (diet , data):
acc =
grams = data.items()
for (name , food , weights) in grams :
if diet = food :
acc.append([weights])
total = sum(acc)
avg = total / len(acc)
return avg

# up above I get a syntax error with the acc.append([weights]) line for some reason

# 3
def word_positions (phrase):
str.split(phrase)
for num in phrase :
return (num + ":" + str(phrase[num])

# I get a random EOF error for some reason.


Thanks guys!
Reply
#2
Quote:
return (num + ":" + str(phrase[num])

# I get a random EOF error for some reason.

You open two parens, but only close one.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] new to python hello world code gives errors giobastidas1970 1 1,469 Mar-11-2023, 12:48 PM
Last Post: jefsummers
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 2,021 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unexpected change to a list in a small amount of self-contained code Johno 5 2,882 Mar-15-2020, 05:06 PM
Last Post: jefsummers
  whats wrong with my code? syntax errors r6lay 5 6,517 Mar-16-2017, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020