这几节开始涉及Python函数,与之前所学的语言不同,Python使用缩进来划分代码块,就有些不习惯。可是一旦你接收了这种设定,就会感到莫名的愉悦“看!我的代码多么规整”之类的。
ex11.py
print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weight?",
weight = raw_input()
print "So, you,re %r old,%r tall and %r heavy." %( age, height, weight)
ex13.py
from sys import argv
script, first, second, third = argv
print "The script is called:",script
print "Your first variable is:",first
print "Your second varisble is:",second
print "Your third variable is:",third
x = raw_input("? ")
print x
ex14.py
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 quertions."
print "Do you like me %s? " % user_name
likes = raw_input(prompt)
print "Where do you 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. Not sure where that is. And you have a %r computer. Nice. """ % (likes, lives, computer)
ex15.py
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
txt.close()
print "Type the filename again:"
file_again = raw_input(">")
txt_again = open(file_again)
print txt_again.read()
txt_again.close()
ex16.py
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, 'a')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
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()
ex17.py
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)
in_file = open(from_file)
indata = in_file.read()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()
out_file = open(to_file,'w')
out_file.write(indata)
print "Alright, all done."
out_file.close()
in_file.close()
ex18.py
# this one is like your scripts 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 nothing."
print_two("Zed","Shae")
print_two_again("Zed","Shae")
print_one("First!")
print_none()
ex20.py
import sys
script, infile = sys.argv
def print_all(f):
print f.read()
def rewind(f):
f.seek(0)
def print_a_line(line, f):
print line, f.readline(),
current = open(infile)
print "First let's print the whole file:\n"
print_all(current)
print "Now let's rewind,kind of like a tape."
rewind(current)
print "Let's print three lines:"
print_a_line(1,current)
print_a_line(2,current)
print_a_line(3,current)
ex24.py
print "Let't 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 * 500
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)
ex25.py
def break_words(stuff):
words = stuff.split(' ')
return words
def sort_words(words):
return sorted(words)
def print_first_word(words):
word = words.pop(0)
print word
def print_last_word(words):
word = words.pop(-1)
print word
def sort_sentence(sentence):
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)