Re: [Tutor] Help with return results statement.

2015-10-22 Thread Vusa Moyo
Hi Guys. Thanks for the responses and assistance.

I came right after-all. The last 2 blocks read the following.


def get_class_average(students):
results = []
for a in students:
b = float(get_average(a))
results.append(b)
return average(results)


students = [lloyd, alice, tyler]
print(get_class_average(students))
bother = get_class_average([lloyd, alice, tyler])
print(get_letter_grade(bother))

Worked like a charm.

Thanks for the assistance.

Regards

Vusa


On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld 
wrote:

> On 20/10/15 12:29, Vusa Moyo wrote:
>
>> Hi there. My script is as follows,
>>
>
> alice = {
>>  "name": "Alice",
>>  "homework": [100.0, 92.0, 98.0, 100.0],
>>  "quizzes": [82.0, 83.0, 91.0],
>>  "tests": [89.0, 97.0]
>> }
>>
>
> # Add your function below!
>> def average(numbers):
>>
> >total = sum(numbers)
> >total = float(total)
>
> That line isn't necessary since the inputs are floats already.
>
> >total = total / len(numbers)
> >return total
> >
>
>> def get_average(student):
>>  homework = average(student["homework"])
>>  quizzes = average(student["quizzes"])
>>  tests = average(student["tests"])
>>  return 0.1 * homework + 0.3 * quizzes + 0.6 * tests
>>
>> def get_letter_grade(score):
>>  if score >= 90:
>>  return "A"
>>  elif score >= 80:
>>  return "B"
>>
>
> print get_average(lloyd)
>>
>> def get_class_average(students):
>>  results = []
>>  for a in students:
>>  b = int(get_average(a))
>>  results.append([b])
>>  return results
>>
>>
> get_class_average(alice)
>>
>> I receive a zero value for results, which doesnt quite make sense to me.
>>
>
> Nor to me. Are you sure its a zero result? It should be a list of some
> kind not a number. Or do you mean you get an empty list back?
>
> Notice that get_class_average() expects your students value to be
> some kind of sequence or collection. The for loop will iterate over that.
> If you pass Alice as a single student it will iterate over the keys, trying
> first of all to get the average of "Alice" which should fail with an error.
> Did you get any errors? If so please let us see them.
>
> Please show us the actual code you execute, the actual output
> and the full text of any errors.
>
> One other thing that seems weird to me is that you go to great pains to
> produce a float as a result of get_average() but then you
> immediately convert it to an int. Why not leave it as a float?
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with return results statement.

2015-10-22 Thread Alan Gauld

On 22/10/15 10:03, Vusa Moyo wrote:

Hi Guys. Thanks for the responses and assistance.

I came right after-all. The last 2 blocks read the following.


Glad you are happy but there is still something you could improve:


def get_class_average(students):
results = []
for a in students:
b = float(get_average(a))


You really don't need the float() here. get_average() is guaranteed
to return a float.


results.append(b)
return average(results)


students = [lloyd, alice, tyler]
print(get_class_average(students))
bother = get_class_average([lloyd, alice, tyler])
print(get_letter_grade(bother))

Worked like a charm.

Thanks for the assistance.

Regards

Vusa


On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld > wrote:


On 20/10/15 12:29, Vusa Moyo wrote:

Hi there. My script is as follows,


alice = {
 "name": "Alice",
 "homework": [100.0, 92.0, 98.0, 100.0],
 "quizzes": [82.0, 83.0, 91.0],
 "tests": [89.0, 97.0]
}


# Add your function below!
def average(numbers):

>total = sum(numbers)
>total = float(total)

That line isn't necessary since the inputs are floats already.

>total = total / len(numbers)
>return total
>

def get_average(student):
 homework = average(student["homework"])
 quizzes = average(student["quizzes"])
 tests = average(student["tests"])
 return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

def get_letter_grade(score):
 if score >= 90:
 return "A"
 elif score >= 80:
 return "B"


print get_average(lloyd)

def get_class_average(students):
 results = []
 for a in students:
 b = int(get_average(a))
 results.append([b])
 return results


get_class_average(alice)

I receive a zero value for results, which doesnt quite make
sense to me.


Nor to me. Are you sure its a zero result? It should be a list of
some kind not a number. Or do you mean you get an empty list back?

Notice that get_class_average() expects your students value to be
some kind of sequence or collection. The for loop will iterate
over that. If you pass Alice as a single student it will iterate
over the keys, trying first of all to get the average of "Alice"
which should fail with an error. Did you get any errors? If so
please let us see them.

Please show us the actual code you execute, the actual output
and the full text of any errors.

One other thing that seems weird to me is that you go to great
pains to produce a float as a result of get_average() but then you
immediately convert it to an int. Why not leave it as a float?


-- 
Alan G

Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



___
Tutor maillist  - Tutor@python.org 
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor





--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with return results statement.

2015-10-22 Thread Vusa Moyo
Thanks Alan.

Removed that .

=
# Add your functions below!
def average(numbers):
total = sum(numbers)
total = total / len(numbers)
return total

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"

#Class average

def get_class_average(students):
results = []
for a in students:
b = float(get_average(a))
results.append(b)
return average(results)

# prompt for results.

students = [lloyd, alice, tyler]
print(get_class_average(students))
bother = get_class_average([lloyd, alice, tyler])
print(get_letter_grade(bother))

=


Appreciate it.

Regards

Vusa

On Thu, Oct 22, 2015 at 11:14 AM, Alan Gauld 
wrote:

> On 22/10/15 10:03, Vusa Moyo wrote:
>
>> Hi Guys. Thanks for the responses and assistance.
>>
>> I came right after-all. The last 2 blocks read the following.
>>
>
> Glad you are happy but there is still something you could improve:
>
> def get_class_average(students):
>> results = []
>> for a in students:
>> b = float(get_average(a))
>>
>
> You really don't need the float() here. get_average() is guaranteed
> to return a float.
>
> results.append(b)
>> return average(results)
>>
>>
>> students = [lloyd, alice, tyler]
>> print(get_class_average(students))
>> bother = get_class_average([lloyd, alice, tyler])
>> print(get_letter_grade(bother))
>>
>> Worked like a charm.
>>
>> Thanks for the assistance.
>>
>> Regards
>>
>> Vusa
>>
>>
>> On Tue, Oct 20, 2015 at 2:38 PM, Alan Gauld > > wrote:
>>
>> On 20/10/15 12:29, Vusa Moyo wrote:
>>
>> Hi there. My script is as follows,
>>
>>
>> alice = {
>>  "name": "Alice",
>>  "homework": [100.0, 92.0, 98.0, 100.0],
>>  "quizzes": [82.0, 83.0, 91.0],
>>  "tests": [89.0, 97.0]
>> }
>>
>>
>> # Add your function below!
>> def average(numbers):
>>
>> >total = sum(numbers)
>> >total = float(total)
>>
>> That line isn't necessary since the inputs are floats already.
>>
>> >total = total / len(numbers)
>> >return total
>> >
>>
>> def get_average(student):
>>  homework = average(student["homework"])
>>  quizzes = average(student["quizzes"])
>>  tests = average(student["tests"])
>>  return 0.1 * homework + 0.3 * quizzes + 0.6 * tests
>>
>> def get_letter_grade(score):
>>  if score >= 90:
>>  return "A"
>>  elif score >= 80:
>>  return "B"
>>
>>
>> print get_average(lloyd)
>>
>> def get_class_average(students):
>>  results = []
>>  for a in students:
>>  b = int(get_average(a))
>>  results.append([b])
>>  return results
>>
>>
>> get_class_average(alice)
>>
>> I receive a zero value for results, which doesnt quite make
>> sense to me.
>>
>>
>> Nor to me. Are you sure its a zero result? It should be a list of
>> some kind not a number. Or do you mean you get an empty list back?
>>
>> Notice that get_class_average() expects your students value to be
>> some kind of sequence or collection. The for loop will iterate
>> over that. If you pass Alice as a single student it will iterate
>> over the keys, trying first of all to get the average of "Alice"
>> which should fail with an error. Did you get any errors? If so
>> please let us see them.
>>
>> Please show us the actual code you execute, the actual output
>> and the full text of any errors.
>>
>> One other thing that seems weird to me is that you go to great
>> pains to produce a float as a result of get_average() but then you
>> immediately convert it to an int. Why not leave it as a float?
>>
>>
>> -- Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>>
>> ___
>> Tutor maillist  - Tutor@python.org 
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
___
Tutor mailli

Re: [Tutor] Help with return results statement.

2015-10-22 Thread Danny Yoo
An additional suggestion:

> =
> # Add your functions below!
> def average(numbers):
> total = sum(numbers)
> total = total / len(numbers)
> return total

Don't re-assign total here.  The problem is that conceptually "total" no
longer represents the total in the second assignment.  It makes the name
meaningless.

One way to resolve the problem is to just return the value.

#
def average(numbers):
total = sum(numbers)
return total / len(numbers)
#

That is, the problem isn't one of computation, but of compassion.  Help
make code readable.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Create complex dictionary

2015-10-22 Thread jarod_v6--- via Tutor
Hi!!I would like to prepare a dictionary with complex structure:

complex = {name ="value",surname="po",age=poi)
 What is the most pythonic way to build   a dictionary of dictionary?thanks for 
any help!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create complex dictionary :p:

2015-10-22 Thread Thomas C. Hicks

On 10/23/2015 05:19 AM, jarod_v6--- via Tutor wrote:

Hi!!I would like to prepare a dictionary with complex structure:

complex = {name ="value",surname="po",age=poi)
  What is the most pythonic way to build   a dictionary of dictionary?thanks 
for any help!


This doesn't look too complex so I am probably missing something.

The normal dictionary construction would look something like this:

mydict = dict('name'='value', 'surname'='po','age'='poi')

Then you can access any given item in mydict with the get method:

mydict.get('name')

SDG,

tom
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create complex dictionary :p:

2015-10-22 Thread Alex Kleider

On 2015-10-22 14:50, Thomas C. Hicks wrote:

On 10/23/2015 05:19 AM, jarod_v6--- via Tutor wrote:

Hi!!I would like to prepare a dictionary with complex structure:

complex = {name ="value",surname="po",age=poi)
  What is the most pythonic way to build   a dictionary of 
dictionary?thanks for any help!



This doesn't look too complex so I am probably missing something.

The normal dictionary construction would look something like this:

mydict = dict('name'='value', 'surname'='po','age'='poi')

Then you can access any given item in mydict with the get method:

mydict.get('name')

SDG,

tom


alex@x301:~$ python3
Python 3.4.3 (default, Jul 28 2015, 18:24:59)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

mydict = dict('name'='value', 'surname'='po','age'='poi')

  File "", line 1
SyntaxError: keyword can't be an expression




my understanding is that you could have done it in either of the 
following two ways:

1:mydict = dict(name='value', surname='po',age='poi')
2:mydict = {'name': 'value', 'surname': 'po','age': 'poi'}

Also, accessing any given item might be done as follows:
mydict['name']
rather than calling the get method, n'est pas?


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create complex dictionary

2015-10-22 Thread Cameron Simpson

On 22Oct2015 23:19, jarod...@libero.it  wrote:

Hi!!I would like to prepare a dictionary with complex structure:

complex = {name ="value",surname="po",age=poi)


Well, in Python you'd write the above like this:

 complex = {'name': "value", 'surname': "po", 'age': poi}


What is the most pythonic way to build   a dictionary of dictionary?


Your description is a bit vague, but it sounds like a dictionary or 
dictionaries is a reasonable way to do it. Example:


 records = {}
 complex = {'name': "value", 'surname': "po", 'age': poi}
 records['key1'] = complex
 complex = {'name': "value2", 'surname': "xy", 'age': poi2}
 records['key2'] = complex

Nothing wrong with that if it fits what you actually need to do.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create complex dictionary

2015-10-22 Thread Ben Finney
jarod_v6--- via Tutor  writes:

> Hi!!I would like to prepare a dictionary with complex structure:
>
> complex = {name ="value",surname="po",age=poi)

That's invalid syntax (the braces don't match) and it seems to use names
from elsewhere.

If you mean a dictionary like this::

wibble = {'name': "value", 'surname': "po", 'age': 42}

then that is a simple dictionary. So I don't know what you mean by a
“complex” dictionary.

> What is the most pythonic way to build   a dictionary of
> dictionary?

You can assign any value to any key in a dictionary. Dictionaries are
also values, so a dictionary can be assigned to a key just like any
other value can be assigned to a key.

wobble = {
'name': "Lorem Ipsum",
'age': 42,
'ratings': {
'badminton': 17.48,
'celery': None,
'fussball': 0.14,
},
'address': "175 West Arglbargle, Lower Snootbatten",
}

That has values that are themselves containers, so I suppose it counts
as a “complex” dictionary.

-- 
 \ “I went to the museum where they had all the heads and arms |
  `\  from the statues that are in all the other museums.” —Steven |
_o__)   Wright |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Create complex dictionary :p: :p:

2015-10-22 Thread Thomas C. Hicks

On 10/23/2015 05:59 AM, Alex Kleider wrote:
mydict = dict('name'='value', 'surname'='po','age'='poi') 
Oops, you are correct! Don't want to put the key names in quotes, I 
mistyped my experiment.


SDG,

tom
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Scraping Wikipedia Table (The retruned file is empty)

2015-10-22 Thread Cynthia Alice Andrews
At this point I feel like I am wasting my time by not asking for help. I
can't figure out why the file keeps coming back empty. There are no error
message, just an empy file. Very frustrating.

from BeautifulSoup import BeautifulSoup
import urllib2
import csv

wiki = "https://en.wikipedia.org/wiki/List_of_Golden_Globe_winners";
header = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

drama = ""
musical_comedy = ""
drama_actor = ""
musical_comedy_actor = ""
drama_actress = ""
musical_comedy_actress = ""
director = ""

table = soup.find("table", {"class": "wikitable"})

f = open('output.csv','w')

for row in table.findAll("tr"):
cells = row.findAll("td")
if len(cells) == 7:
drama = cells[0].find(text=True)
musical_comedy = cells[1].find(text = True)
drama_actor = cells[2].find(text=True)
musical_comedy_actor = cells[3].find(text=True)
drama_actress = cells[4].find(text=True)
musical_comedy_actress = cells[5].find(text=True)
director = cells[6].find(text=True)

for x in range(len(drama_actor)):
drama_actor_list = drama_actor[x].split("/")
for i in range(len(drama_actor_list)):
if (len(drama_actor_list[i]) > 2) and (len(drama_actor_list[i]) <=5):
data = drama + "," + musical_comedy + "," +
drama_actor_list[i].lstrip('\n').strip() + "," + musical_comedy_actor + ","
+ drama_actress + "," + musical_comedy_actress + "," + director + "," + "\n"
print write_to_file
f.write(write_to_file)
f.close()


*cynthia*andrews
MCDM Candidate, Communication Leadership
University of Washington

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scraping Wikipedia Table (The retruned file is empty)

2015-10-22 Thread Danny Yoo
On Thu, Oct 22, 2015 at 4:01 PM, Cynthia Alice Andrews
 wrote:
> At this point I feel like I am wasting my time by not asking for help. I
> can't figure out why the file keeps coming back empty. There are no error
> message, just an empy file. Very frustrating.


Unfortunately, the formatting of your code broke a bit.  Can you try
showing us the code again, and make sure that it's in text mode?

On a first glance, I suspect that the code isn't actually even getting
to the point that it's supposed to write to files.  Here's why:
there's a reference to a variable "write_to_file", and I can't tell
where that variable is supposed to be set.  That, plus the fact that
you haven't seen any error messages, most likely means at least two
things:

1.  You probably need to correct the variable from "write_to_file" to
"data", if I read the intention of the program correctly.

2.  You probably need to also correct a bug in the control flow,
because your program isn't reaching the point where it's trying to
write.  Otherwise, you would have seen a runtime error when Python
tries to use the name "write_to_file" which hasn't been defined
anywhere.



Looking more at the code...

> for x in range(len(drama_actor)):

This looks unusual.  drama_actor is just a single string: ranging
across the characters of a string looks very unusual.  Are you sure
you want to do this?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scraping Wikipedia Table (The retruned file is empty)

2015-10-22 Thread Danny Yoo
On Thu, Oct 22, 2015 at 8:07 PM, Personal  wrote:
> I figured it out! Thanks for taking the time to respond!


No problem; glad you found it.  :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor