[Courses] [python] Lesson 6: Functions and dictionaries
Annie Desaulniers
annie_desaulniers at skynet.be
Wed Jul 27 13:13:28 UTC 2011
Hi,
Here my homeworks.
Annie
Lesson 6
1 .
#!/usr/bin/env python
import random
def pickin(dict) :
key = random.choice(dict.keys())
value = dict[key]
return (key, value)
urls = { "LinuxChix" : "http://linuxchix.org",
"Ubuntu Women" : "http://ubuntu-women.org/",
"Debian Women" : "http://women.debian.org/",
"Geek Feminism" : "http://geekfeminism.wikia.com/"
}
print pickin(urls)
2.
#!/usr/bin/env python
import random
def pickin(dict) :
key = random.choice(dict.keys())
value = dict[key]
return (key, value)
questions = { " 5 + 5 / 2 * 3" : " 15 ",
" 27 / 3 + 5 " : " 14 "
}
cmd = ""
while cmd != "q" :
print pickin(questions)[0] +" "
cmd = raw_input()
3. I think using strip() could already help to remove head and tail
blanks. If the questions are text we could also see to put all in lower
case and eventually remove every spaces.
import random
def pickin(dict) :
key = random.choice(dict.keys())
value = dict[key]
return (key, value)
questions = { "5 + 5 / 2 * 3" : "15",
"27 / 3 + 5 " : "14"
}
cmd = ""
i = 0
j = 0
while cmd != "q":
i = i + 1
key = pickin(questions)[0]
value = questions[key]
print key
cmd = raw_input()
while cmd.strip() != value :
if cmd == "q":
break
cmd = raw_input("try again \n")
if cmd.strip() == value :
print "correct"
j = j + 1
if cmd == "q":
print j ," good answers on " ,i , " questions"
Lesson 5
1. x is the number of the current item in my list of things and y is the
number of colors
colors = [ "red", "orange", "yellow", "green", "blue" ]
listofthings = [
"table","desk","chair","computer","lamp","printer","pen","book","marker","cisors",
"cable","phone","watch","speaker","pencil","paper","clock","screen","mouse","keyboard"]
for i in range(0,20):
print i % 5
print colors[i % 5] +" "+ listofthings[i]
2 .
#!/usr/bin/env python
import os
import sys
import random
listoffile = os.listdir(sys.argv[1])
print len(listoffile)
print random.randint(0,len(listoffile))
print listoffile[random.randint(0,len(listoffile))]
3.
import random
listofthings = [
"table","desk","chair","computer","lamp","printer","pen","book","marker","cisors",
"cable","phone","watch","speaker","pencil","paper","clock","screen","mouse","keyboard"]
listofverbs = [ "do", "fit", "burn", "fall" ]
print listofthings[random.randint(0,len(listofthings)-1)]+"
"+listofverbs[random.randint(0,len(listofverbs)-1)]
4.
import random
import os
colors = [ "red", "orange", "yellow", "green", "blue" ]
listofthings = [
"table","desk","chair","computer","lamp","printer","pen","book","marker","cisors",
"cable","phone","watch","speaker","pencil","paper","clock","screen","mouse","keyboard"]
listofiverbs = [ "do", "fit", "burn", "fall" ]
listofverbs = [ "chase", "run", "sleep", "walk" ]
listofnouns = [ "dog", "cat", "spider", "human"]
cmd = " "
while cmd != "quit" :
cmd = raw_input("Command? ")
if cmd == "verb":
verb = raw_input("Verb? ")
print listofnouns[random.randint(0,len(listofnouns)-1)]+"
"+verb+" "+colors[random.randint(0,4) ]+"
"+listofthings[random.randint(0,len(listofthings)-1)]
if cmd == "go" :
typeofsentence = random.randint(0,1)
if typeofsentence == 0:
print colors[random.randint(0,4) ]+"
"+listofthings[random.randint(0,len(listofthings)-1)]+"
"+listofiverbs[random.randint(0,len(listofiverbs)-1)]
else:
print listofnouns[random.randint(0,len(listofnouns)-1)]+"
"+listofverbs[random.randint(0,len(listofverbs)-1)]
Lesson 4
1.
# When the user entered no argument or not a valid int,
# the interpreter gives an error because there is nothing
# to parse as int or it is not possible to parse it.
# To check if the user entered a value we can check the lenght of argv
# and it has to be bigger then 1 as the first argument is in the second
index
# If there is an argument given when can try to parse it to int and
when this
# doesn't work we print an error message
# else we give an error message saying there is no argument
import sys # give the arguments + name of file arg[0]
if len(sys.argv) > 1 :
try :
num = int(sys.argv[1])
for i in range(0, num):
print i
except :
print "Error : impossible to parse "+sys.argv[1]+" to integer"
else :
print "Error : empty argument"
2.
import sys
try:
file = open(sys.argv[1])
n = 0
for line in file:
n = n+1
#print "Read a line:",line,n
print n
file.close()
except:
print "Error"
3.
n = 0
try:
for arg in sys.argv[1:]:
file = open(arg)
for line in file:
n = n+1
print n
file.close()
except:
print "Error"
4. a.
import sys
n = 0
try:
for arg in sys.argv[1:]:
file = open(arg)
for line in file:
mystring = str(line)
mylist = mystring.split(" ")
n = n + len(mylist)
file.close()
print "total word count =", n
except:
print "Error"
b. no the answers are not the same for files with empty spaces
c. my program counts the empty spaces
d.
n = 0
try:
for arg in sys.argv[1:]:
file = open(arg)
for line in file:
mystring = str(line)
# print mystring
mystring = mystring.strip()
mylist = mystring.split(" ")
# print len(mylist)
n = n + len(mylist)
# print n
file.close()
print "total word count =", n
except:
print "Error"
Lesson 3
1. how to count words
mystring = "first second third forth fifth"
lst = mystring.split(" ")
print mystring,"size of the list =", len(lst)
2. the list index with a minus value is taken after the lenght, lst[-1]
= last index, lst[-2] = the one before the last but the index can't be
out of the range of +/- lenght.
print lst[-1],"is the same as =", lst[4]
print lst[-2],"is the same as =", lst[len(lst)-2]
print lst[-5],"is the same as =", lst[len(lst)-5]
3. Guido van Rossum is the creator of python, he has a multipart name
and the "van" part is with a lower case letter
4. skip
5. the histogram
vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]
for i in range(0,len(vals)):
starstring = ""
for j in range(0,vals[i]):
starstring = starstring + "*"
print starstring
for i, item in enumerate(vals):
starstring = ""
for j in range(0,vals[i]):
starstring = starstring + "*"
print starstring
More information about the Courses
mailing list