21 Code:
defadd(a,b):
print("ADDing %d+%d"%(a,b))
returna+b
defsubstract(a,b):
print("SUBSTRACT %d*%d"%(a,b))
returna-b
defdivide(a,b):
print("DIVIDE %d/%d"%(a,b))
returna/b
defmultiply(a,b):
returna*b
print("Let's do some math with just function!")
age=add(30,5)
weight=substract(78,4)
height=multiply(90,2)
iq=divide(100,2)
print("Age:%d,height:%d,weight:%d,IQ:%d"%(age,height,weight,iq))
what=add(age,substract(height,multiply(weight ,divide(iq,2))))
print("That becomes:,what,Can you do it by hand")
24Code:
print("Let's practice everything.")
print("You\'d need to know\'bout escape with\\that do\nnewlines and\ttabs")
poem="""
\tThe lovely world
with logic so firmly planted
cannot discern\nthe needs of love
nor comprehend passion from intuition
and requires and explanation
\n\t\twhere there is none.
"""
print("-----------------")
print(poem)
print("---------")
five=10-2+3-6
print("This should be five:%s"%five)
defsecret_formula(started):
jelly_beans=started*500
jars=jelly_beans/1000
crates=jars/100
returnjelly_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 scates."%(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))
25Code:
defbreak_words(stuff):
"""This function will break up words us."""
words=stuff.split('')
returnwords
defsort_words(words):
"""Sorts the words"""
returnsorted(words)
defprint_first_word(words):
"""Print the first word after popping it off."""
word=words.pop(0)
print(word)
defprint_last_word(words):
"""Print the last word after popping it off."""
word=words.pop(-1)
print(word)
defsort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words=break_words(sentence)
returnsort_words(words)
defprint_first_and_last(sentence):
"""Print the first and last words of the sentence."""
words=break_words(sentence)
print_first_word(words)
print_last_word(words)
defprint_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)
26Code:
defbreak_words(stuff):
"""This function will break up words for us."""
words=stuff.split(' ')
returnwords
defsort_words(words):
"""Sorts the words."""
returnsorted(words)
defprint_first_word(words):
"""Prints the first word after popping it off."""
word=words.poop(0)
print(word)
defprint_last_word(words):
"""Prints the last word after popping it off."""
word=words.pop(-1)
print(word)
defsort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words=break_words(sentence)
returnsort_words(words)
defprint_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)
defprint_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)
print("Let's practice everything.")
print('You\'d need to know\'bout escapes with\\that do\nnewlines and\ttabs.')
poem="""
\tThe lovely world
with logic so firmly planted
cannot discern\nthe needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""
print("--------------")
print(poem)
print("--------------")
five=10-2+3-6
print("This should be five: %s"%five)
defsecret_formula(started):
jelly_beans=started*500
jars=jelly_beans/1000
crates=jars/100
returnjelly_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 jeans, %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 crabapples."%secret_formula(start_point))
sentence="All god\tthings come to those who weight."
words=ex25.break_words(sentence)
sorted_words=ex25.sort_words(words)
print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words=ex25.sort_sentence(sentence)
print(sorted_words)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)
29Code:
people=20
cats=30
dogs=15
ifpeople
print("Too many cats!The world is doomed!")
ifpeople>cats:
print("Not many cats!The world is doomed!")
ifpeople
print("The world is drooled on!")
ifpeople>dogs:
print("The world is dry")
dogs+=5
ifpeople>=dogs:
print("people greater than or equal to dogs.")
ifpeople<=dogs:
print("people are less than or equal to dogs.")
ifpeople==dogs:
print("people are dogs.")
30Code:
people=30
cars=40
buses=15
ifcars>people:
print("We should take the cars.")
elifcars
print("We should not take the cars.")
elif:
print("We can't decide.")
ifbuses>cars:
print("That's too many buses.")
elifbuses
print("Maybe we could take the buses.")
else:
print("We still can't decide.")
ifpeople>buses:
print("Alright, let's just take the buses.")
else:
print("Fine, let's stay home then.")
网友评论