Difference between revisions of "CSC111 Lab4 Solutions"
(Created page with '--~~~~ ---- <source lang="python"> import os def main1(): Id = raw_input( "Please enter Id of student (e.g. aa) " ) command = "111c-" + Id os.system( "lastlog -u…') |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
---- | ---- | ||
<source lang="python"> | <source lang="python"> | ||
+ | # Selected solutions for Lab 4 | ||
import os | import os | ||
Line 27: | Line 27: | ||
def main5(): | def main5(): | ||
+ | zoo = [ "pig", "horse", "elephant" ] | ||
+ | count = 0 | ||
+ | for animal in zoo: | ||
+ | count = count + 1 | ||
+ | for i in range( count ): | ||
+ | print animal, | ||
+ | print | ||
+ | |||
+ | def main6(): | ||
+ | for year in [ 2010, 2011, 2012, 2013]: | ||
+ | for sem in [ "spring", "fall" ]: | ||
+ | print sem, year | ||
+ | |||
+ | def main7(): | ||
filename = raw_input( "Filename? " ) | filename = raw_input( "Filename? " ) | ||
filenameNoExtension = filename[0:-3] | filenameNoExtension = filename[0:-3] | ||
Line 32: | Line 46: | ||
+ | main1() | ||
+ | main2() | ||
+ | main3() | ||
+ | main4() | ||
main5() | main5() | ||
+ | main6() | ||
+ | main7() | ||
+ | </source> | ||
− | </ | + | <br /> |
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Labs]] |
Latest revision as of 12:56, 24 February 2010
# Selected solutions for Lab 4
import os
def main1():
Id = raw_input( "Please enter Id of student (e.g. aa) " )
command = "111c-" + Id
os.system( "lastlog -u " + command )
def main2():
for Id in "abcdefghijklmnopqrstuvwxyz":
user = "111c-a" + Id
os.system( "lastlog -u " + user )
def main3():
for semester in [ 'a', 'b' ]:
for Id in "abcdefghijklmnopqrstuvwxyz":
user = "111c-" + semester + Id
os.system( "lastlog -u " + user )
def main4():
for second in "abcdefghijklmnopqrstuvwxyz":
for first in "abc":
print first+second, # comma to stay on the same line
print # once we've printed 3, go to next line
def main5():
zoo = [ "pig", "horse", "elephant" ]
count = 0
for animal in zoo:
count = count + 1
for i in range( count ):
print animal,
print
def main6():
for year in [ 2010, 2011, 2012, 2013]:
for sem in [ "spring", "fall" ]:
print sem, year
def main7():
filename = raw_input( "Filename? " )
filenameNoExtension = filename[0:-3]
print filenameNoExtension + "txt"
main1()
main2()
main3()
main4()
main5()
main6()
main7()