习题21-26.md

def add(a,b):
    print("ADDING%d+%d\n"%(a,b))
    return a+b

def subtract(a,b):
    print("SUBSTRACT%d-%d\n"%(a,b))
    return a-b

def multiply(a,b):
    print("MULTIPLY%d*%d\n"%(a,b))
    return a*b

def divide(a,b):
    print("DIVIDE%d/%d\n"%(a,b))
    return a/b

print("Let's do some math with just functions")

age = add(30,8)
height = subtract(78,4)
weight = multiply(90,5)
iq = divide(80,2)

print("age:%d,height:%d,weight:%d,iq:%d"%(age,height,weight,iq))

print("Here is a puzzle")

what = add(age,subtract(height,multiply(weight,divide(iq,2))))

print("That become:",what,"can you do it by hand?")
Let's do some math with just functions
ADDING30+8

SUBSTRACT78-4

MULTIPLY90*5

DIVIDE80/2

age:38,height:74,weight:450,iq:40
Here is a puzzle
DIVIDE40/2

MULTIPLY450*20

SUBSTRACT74-9000

ADDING38+-8926

That become: -8888.0 can you do it by hand?
print("Let's practice everything.")
print('You\'d need to known \'bout escapes with \\ that do \n newline and \t tabs')

poem = """
\t The 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,%d crates."%(beans,jars,crates))

start_point = start_point/10

print("We can also do that this way:")
print("We'd have %d beans,%djars,%dcrates."%secret_formula(start_point))

Let's practice everything.
You'd need to known 'bout escapes with \ that do 
 newline and 	 tabs
----------

	 The lovely world
with logic so firmly planted
cannot discern
 the needs of love
nor comprehend passion from intuition
and requires an explanation

		where there is none.

----------
This should be five:5
With a starting point of:10000
We'd have 5000000 beans,5000 jars,50 crates.
We can also do that this way:
We'd have 500000 beans,500jars,5crates.
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):
    """Takes 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_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

sentence = "All good things come to those who wait."

words1 = break_words(sentence)
print(words1)
print(sort_words(words1))
print_first_word(words1)
print_last_word(words1)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
All
wait.
All
who

猜你喜欢

转载自blog.csdn.net/DMU_lzq1996/article/details/82822855