diff --git a/yufree123/.ex17.py.swp b/yufree123/.ex17.py.swp new file mode 100644 index 0000000..e93a586 Binary files /dev/null and b/yufree123/.ex17.py.swp differ diff --git a/yufree123/ex14.py b/yufree123/ex14.py new file mode 100644 index 0000000..850e922 --- /dev/null +++ b/yufree123/ex14.py @@ -0,0 +1,21 @@ +from sys import argv + +script, user_name = argv +prompt = '>' + +print "Hi %s, I'm the %s script."%(user_name,script) +print "I'd like to ask you a few qustions." +print "Do you like me %s?"%user_name +likes=raw_input(prompt) + +print "Where doyou live %s"%user_name +lives = raw_input(prompt) + +print "What kind of computer do you have?" +computer = raw_input(prompt) + +print """ +Alright, so you said %r about liking me. +You live in %r. Noy sure where that is. +And you have a %r computer. Nice. +"""%(likes, lives, computer) diff --git a/yufree123/ex15.py b/yufree123/ex15.py new file mode 100644 index 0000000..bf2a3c5 --- /dev/null +++ b/yufree123/ex15.py @@ -0,0 +1,14 @@ +from sys import argv +script , filename = argv + +txt = open(filename) + +print "Here's your file %r:"%filename +print txt.read() + +print "Type the filename again:" +file_again =raw_input(">") + +txt_again = open(file_again) + +print txt_again.read() diff --git a/yufree123/ex16.py b/yufree123/ex16.py new file mode 100644 index 0000000..eb86785 --- /dev/null +++ b/yufree123/ex16.py @@ -0,0 +1,34 @@ +from sys import argv + +script, filename=argv + +print "We're going to erase %r." % filename +print "If you don't want that, hit CTRL-C(^C)." +print "If you do want that, hit RETURN." + +raw_input("?") + +print "Opening the file..." +target=open(filename,'w') + +print "Truncating the file. Goodbye!" +target.truncate() + +print "Now I'm going to ask you for three lines." + +line1= raw_input("line1:") +line2= raw_input("line2:") +line3= raw_input("line3:") + +print "I'm going to write these to the file." + +target.write(line1) +target.write("\n") +target.write(line2) +target.write("\n") +target.write(line3) +target.write("\n") + +print "And finally,we close it." +target.close() + diff --git a/yufree123/ex17.py b/yufree123/ex17.py new file mode 100644 index 0000000..61dc45c --- /dev/null +++ b/yufree123/ex17.py @@ -0,0 +1,25 @@ +from sys import argv +from os.path import exists +script ,from_file,to_file=argv +print "Copying from %s to %s"%(from_file,to_file) + + +#we could do these two on one line too, how? +in_file= open(from_file) +indata=in_file.read() + +print "Does the output file is %d bytes long" % len(indata) + +print "Does the output file exist?%r"% exists(to_file) +print "Ready, hit RETURN to contiue, CTRL-C to about." +raw_input() + +out_file = open(to_file,'w') +out_file.write(indata) + +print "Alright, all done." + +out_file.close() + + + diff --git a/yufree123/ex17.py~ b/yufree123/ex17.py~ new file mode 100644 index 0000000..de5efd1 --- /dev/null +++ b/yufree123/ex17.py~ @@ -0,0 +1,25 @@ +from sys import argv +from os.path import exists +script ,from_file,to_file=argv +print "Copying from %s to %s"%(from_file,to_ + + +#we could do these two on one line too, how? +in_file= open(from_file) +indata=in_file.read() + +print "Does the output file is %d bytes long" % len(indata) + +print "Does the output file exist?%r"% exists(to_file) +print "Ready, hit RETURN to contiue, CTRL-C to about." +raw_input() + +out_file = open(to_file,'w') +out_file.write(indata) + +print "Alright, all done." + +out_file.close() + + + diff --git a/yufree123/ex18.py b/yufree123/ex18.py new file mode 100644 index 0000000..fa9497a --- /dev/null +++ b/yufree123/ex18.py @@ -0,0 +1,22 @@ +#this one is like your script with argv +def print_two(*args): + arg1,arg2=args + print "arg1:%r,arg2:%r"%(arg1,arg2) + +#ok,that *args is actually pointless,we can just do this +def print_two_again(arg1,arg2): + print "arg1:%r,arg2:%r"%(arg1,arg2) + +#this just takes one argument +def print_one(arg1): + print "arg1:%r"%arg1 + +#this one takes no arguments +def print_none(): + print "I got nothin'." + + +print_two("Zed","Shaw") +print_two_again("Zed","Shaw") +print_one("First!") +print_none() diff --git a/yufree123/ex19.py b/yufree123/ex19.py new file mode 100644 index 0000000..e805e35 --- /dev/null +++ b/yufree123/ex19.py @@ -0,0 +1,23 @@ +def cheese_and_crackers(cheese_count,boxes_of_crackers): + print "You have %d cheese!"%cheese_count + print "You have %d boxes of crackers"%boxes_of_crackers + print "Man that's enough for a party!" + print "Get a blanket.\n" + + +print "We can just give the function numbers directly:" +cheese_and_crackers(20,30) + + +print "OR,we can use variables from our script:" +amount_of_cheese=10 +amount_of_crackers=50 + +cheese_and_crackers(amount_of_cheese,amount_of_crackers) + + +print "We can even do math inside too:" +cheese_and_crackers(10+20,5+6) + +print "And we can combine the two, variables and math:" +cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000) diff --git a/yufree123/ex20.py b/yufree123/ex20.py new file mode 100644 index 0000000..cd1a113 --- /dev/null +++ b/yufree123/ex20.py @@ -0,0 +1,34 @@ +from sys import argv + +script ,input_file=argv + +def print_all(f): + print f.read() + +def rewind(f): + print f.seek(0) + +def print_a_line(line_count,f): + print line_count,f.readline() + +current_file=open(input_file) + +print "First let's print the whole file:\n" + +print_all(current_file) + +print "Now let's rewind, kind of like a tape." + +rewind(current_file) + +print "Let's print three lines:" + +current_line=1 +print_a_line(current_line,current_file) + +current_line=current_line+1 +print_a_line(current_line,current_file) + +current_line=current_line+1 +print_a_line(current_line,current_file) + diff --git a/yufree123/ex21.py b/yufree123/ex21.py new file mode 100644 index 0000000..6c196b4 --- /dev/null +++ b/yufree123/ex21.py @@ -0,0 +1,32 @@ +def add(a,b): + print "ADDING %d+%d"%(a,b) + return a+b + +def subtract(a,b): + print "SUBTRACTING %d-%d"%(a,b) + return a-b; + +def multiply(a,b): + print "MULTIPLYING %d*%d"%(a,b) + return a*b; + +def divide(a,b): + print "DIVIDING %d/%d"%(a,b) + return a/b + + +print "Let's do some math with just functions!" + +age= add(30,5) +height=subtract(78,4) +weight=multiply(90,2) +iq=divide(100,2) + +print "Age:%d,Height:%d,Weight:%d,IQ:%d"%(age,height,weight,iq) + +#A puzzle for the extra credit,type it in anyway. +print "Here is a puzzle." + +what =add(age,subtract(height,multiply(weight,divide(iq,2)))) + +print "That becomes:",what,"Can you do it by hand?" diff --git a/yufree123/ex24.py b/yufree123/ex24.py new file mode 100644 index 0000000..9ad510e --- /dev/null +++ b/yufree123/ex24.py @@ -0,0 +1,36 @@ +print "Let's practice everything." +print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.' +poem =""" +\tThe lovely world +with logic so firmly planted +cannot discern \n the needs of love +nor comprehend passion from intuition +and requires an explanation +\n\t\twhere there is none. +""" + + +print "-------------" +print poem +print "-------------" + +five=10-2+3-6 +print "This should be five: %s"%five + +def secret_formula(started): + jelly_beans=started*50 + jars=jelly_beans/1000 + crates=jars/100 + return jelly_beans,jars,crates + +start_point=10000 +beans,jars,crates=secret_formula(start_point) + +print "With a starting point of: %d"%start_point +print "We'd have %d beans,%d jars, and %d crates."%(beans,jars,crates) + + +start_point=start_point/10 + +print "We can also do that this way:" +print "We'd have %d beans,%d jars, and %d crates."%secret_formula(start_point) diff --git a/yufree123/ex25.py b/yufree123/ex25.py new file mode 100644 index 0000000..9e09392 --- /dev/null +++ b/yufree123/ex25.py @@ -0,0 +1,31 @@ +def break_words(stuff): + """This function will break up words for us.""" + words=stuff.split(' ') + return words +def sort_words(words): + """Sorts the words.""" + return sorted(words) +def print_first_word(words): + """Prints the first word after popping it off.""" + word=words.pop(0) + print word + +def print_last_word(words): + """Prints the last word after popping it off.""" + word=words.pop(-1) + print word + +def sort_sentence(sentence): + """Take in a full sentence and returns the sorted words.""" + words=break_words(sentence) + return sort_words(words) +def print_first_and_last(sentence): + """Prints the first and last words of the sentence.""" + words=break_words(sentence) + print_first_word(words) + print_last_word(words) +def print_first_and_last_sentence(sentence): + """Sorts the words then prints the first and last one.""" + words=sort_sentence(sentence) + print_first_word(words) + print_last_word(words) diff --git a/yufree123/ex29.py b/yufree123/ex29.py new file mode 100644 index 0000000..5e823aa --- /dev/null +++ b/yufree123/ex29.py @@ -0,0 +1,21 @@ +people=20 +cats=30 +dogs=15 + +if peoplecats: + print "Not many cats! The world is saved!" +if peopledogs: + print "The world is dry!" +dogs+=5 +if people>=dogs: + print "People are greater than or equal to dogs." +if people<=dogs: + print "People are less than or equal to dogs." + +if people==dogs: + print "People are dogs." + diff --git a/yufree123/ex30.py b/yufree123/ex30.py new file mode 100644 index 0000000..991b301 --- /dev/null +++ b/yufree123/ex30.py @@ -0,0 +1,25 @@ +people = 30 +cars = 40 +buses = 15 + + +if cars>people: + print "We should take the cars." +elif cars < people: + print "We should not take the cars." +else: + print "We can't decide." + +if buses > cars: + print "That's too many buses." +elif buses buses: + print "Alright, let's just take the buses." +else: + print "Fine,let's stay home then." + + diff --git a/yufree123/ex31.py b/yufree123/ex31.py new file mode 100644 index 0000000..d5dc58c --- /dev/null +++ b/yufree123/ex31.py @@ -0,0 +1,32 @@ +print "Your enter a dark room with two doors. Do you go thriugh door #1 or door#2?" + +door = raw_input(">") + +if door == "1": + print "There's a giant bear here eating a cheese cake. What do you do?" + print "1. Take the cake." + print "2. Scream at the bear." + + bear = raw_input(">") + + if bear=="1": + print "The bear eats your face off. Good job!" + elif bear=="2": + print "The bear eats your legs off. Good job!" + else: + print "Well, doing %s is probably better. Good job." + +elif door =="2": + print "You stare into the endless abyes at Cthulhu's retia." + print "1. Blueberries." + print "2. Yellow jacket clothespins." + print "3. Understanding revolvers yelling melodies." + + insanity= raw_input(">") + + if insanity=="1"or insanity=="2": + print "Yoour body survives powered by a mind of jello. Good job!" + else: + print "The insanity rots your eyes into a pool of muck. Good job!" +else: + print "You stumble around and fall on a knife and die. Good job!" diff --git a/yufree123/ex32.py b/yufree123/ex32.py new file mode 100644 index 0000000..8c2fcde --- /dev/null +++ b/yufree123/ex32.py @@ -0,0 +1,27 @@ +the_count= [1,2,3,4,5] +fruits=['apples','oranges','pears','apricots'] +change=[1,'pennies',2,'dimes',3,'quarters'] + +# this first kind of for-loop goes through a list +for number in the_count: + print "This is count %d"%number + +# same as above +for fruit in fruits: + print "A fruit of type:%s"%fruit + +#also we can go throuth mixed lists too +# notice we have to use %r since we don't know what's in it +for i in change: + print "I got %r "%i +#We can also build lists,ffirst starts with an empty one +elements=[] + +#then use the range function to do 0 to 5 counts +for i in range(0,6): + print "Adding %d to the list."%i + #append is a function that lists understanding + elements.append(i) +#now we can print them out too +for i in elements: + print "Elements was: %d"%i diff --git a/yufree123/ex33.py b/yufree123/ex33.py new file mode 100644 index 0000000..e542472 --- /dev/null +++ b/yufree123/ex33.py @@ -0,0 +1,11 @@ +i=0 +numbers=[] +while i<6: + print "At the top i is %d"%i + numbers.append(i) + i=i+1 + print "Number now:",numbers + print "At the bottom i is %d"%i +print "The numbers" +for num in numbers: + print num diff --git a/yufree123/ex35.py b/yufree123/ex35.py new file mode 100644 index 0000000..aae5254 --- /dev/null +++ b/yufree123/ex35.py @@ -0,0 +1,76 @@ +from sys import exit + +def gold_room(): + print "This room is full of gold. How much do you take?" + + next = raw_input("> ") + if"0"in next or "1" in next: + how_much=int(next) + else: + dead("Man,Learn to type a number.") + + if how_much<50: + print "Nice, you're not greedy, you win." + exit(0) + else: + dead("You greedy baastard!") +def bear_room(): + print "There is a bear here." + print "The bear has a bunch of honey." + print "The fat bear is in front of another door." + print "How are you going to move the bear?" + bear_moved=False + + while True: + next= raw_input("> ") + + if next=="take honey": + dead("The bear looks at you then slaps your face off.") + elif next=="taunt bear"and not bear_moved: + print "The bear has moved from the door. You can go throuth it now." + bear_moved=True + elif next=="tanunt bear"and not bear_moved: + dead("The bear gets pissed off and chews your leg off.") + elif next=="open door"and bear_moved: + gold_room() + else: + print "I got no idea what that mean." + + +def cthulhu_room(): + print "Here you see the great ebil Cthulhu." + print "He,it,whatever stares at you and you go insane." + print "Do you flee for your life or eat your head?" + + next= raw_input("> ") + + if "flee"in next: + start() + elif"head"in next: + dead("Well that was tasty!") + else: + cthulhu_room() + + +def dead(why): + print why,"Good job!" + exit(0) + +def start(): + print "You are in a dark room." + print "There is a door to your right and left." + print "Which one do you take?" + + next=raw_input("> ") + + if next=="left": + bear_room() + elif next=="right": + cthulhu_room() + else : + dead("You stumble around the room until you atarve.") + +start() + + + diff --git a/yufree123/ex38.py b/yufree123/ex38.py new file mode 100644 index 0000000..a1439da --- /dev/null +++ b/yufree123/ex38.py @@ -0,0 +1,21 @@ +ten_things="Apples Oranges Crows Telephone Light Suger" + +print "Wait there's not 10 things inthat list, lte's fix that." + +stuff=ten_things.split(' ') +more_stuff=["Day","Nignt","Song","Frisbee","Corn","Banana","Girl","Boy"] + +while len(stuff)!=10: + next_one=more_stuff.pop() + print "Adding:",next_one + stuff.append(next_one) + print "There's %d items now."%len(stuff) +print "There we go:",stuff + +print "Let's do some things with stuff." + +print stuff +print stuff[-1]#whoa!fancy +print stuff.pop() +print ' '.join(stuff)#what?cool! +print "#".join(stuff[3:5])#super stellar! diff --git a/yufree123/test.txt~ b/yufree123/test.txt~ new file mode 100644 index 0000000..e69de29 diff --git "a/yufree123/\346\227\240\346\240\207\351\242\230\346\226\207\346\241\243~" "b/yufree123/\346\227\240\346\240\207\351\242\230\346\226\207\346\241\243~" new file mode 100644 index 0000000..e69de29