python学习第八天
习题25 更多更多的练习
def break_words(stuff):
"""this funtion wwill break up words for us.""" #注释
words = stuff.split(' ') #split(' ')函数,分隔字符串,以空格分隔
return words
def sort_words(words):
return sorted(words) #sorted()函数 进行排序
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)
在python中调用脚本,如果在同一文件夹内,直接import,如不在同一文件夹则进行如下输入
import sys
sys.path.insert(0,‘f:/lpthw’)
然后继续编码即可,这个习题里面的新东西包括split()分割字符串函数,sorted()排序函数,pop
()随机移除一个字符,不放参数时默认为最后一个,0代表正序第一个一次往后,-1代表逆序第一个.这个习题要重点注意在脚本中调用脚本不同文件夹下时输入的代码。
习题26 考试
本习题主要是对他所给的程序进行改错,都是一些基本的错误如变量名不一致,缺少括号,个别函数用法错误等
习题27、28
这两个习题学习了逻辑运算及布尔表达式的练习,要注意逻辑表达式的处理方法
习题29 if语句
people =int(input('people\t'))
cats = int(input('cats\t'))
dogs = int(input('dogs\t'))
if people < cats:
print("too many cats ! the world is doomed")
if people > cats:
print("not many cats! the world is saved")
if people < dogs:
print("the world is drooled on")
if people > dogs:
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")
本习题主要是if语句的练习使用
习题30 else和if
people =int(input('people\t'))
cars = int(input('cars\t'))
trucks = int(input('trucks\t'))
if cars > people:
print("we shuold take the cars")
elif cars < people:
print("we should not take the cars")
else:
print("we can't decide")
if trucks > cars:
print("that's too many trucks")
elif trucks < cars:
print("maybe we could take the trucks")
else:
print("we still can't decide")
if people > trucks:
print("alright, let's just take the trucks.")
else:
print("fine, let's stay home then")
此习题是if elfi else 的用法练习
习题31 做出决定
这个习题主要是运用if elif else 语句来做一个简单的文字冒险游戏,由于英文的可读性太差,这里我自己改变了一个简单的文字冒险游戏
print("""you enter a dark room with two doors.
do you go through door #1 or door #2? """)
door = input(">>>> ")
if door == "1":
print("这里有一只小松鼠再吃松仁玉米")
print("你想要怎么做")
print("1.吃掉松仁玉米")
print("2.抓住小松鼠")
bear = input('>>>> ')
if bear == "1":
print("这只小松鼠叫来了他的爸爸妈妈兄弟姐妹把你打跑了")
elif bear == "2":
print("你成功收获一只小松鼠")
else:
print(f"好的 {bear} 或许会更好.")
print("你即吃到了松仁玉米也抓到了小松鼠")
elif door == "2":
print("你看到一只大脑斧在吃午饭.")
print("1. 去拽他的尾巴")
print("2. 去抢他的饭")
print("3. 掉头逃跑")
insanity = input(">>>> ")
if insanity == "1" or insanity == "2":
print("你与他生死搏斗,结果输了,你被他吃掉了")
print("干的漂亮")
else:
print("你成功的活了下来")
#print("good job")
else:
print("你打开了宝藏的大门,干的漂亮")
习题32 循环和列表
本习题详细的介绍了列表的基本概念,讲述了列表的一些基本操作,本习题需要注意的是新出现的函数用法,及for循环
the_count = [1, 2, 3, 4, 5] #创建列表
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
for number in the_count: #for循环语句,将列表the_count里的东西付给变量number
print(f"this is count {number}")
for fruit in fruits:
print(f"a fruit of type: {fruit}")
for i in change:
print(f"i got {i}")
elements = [] #创建一个空列表
for i in range(0, 6): #for循环将0到5五个数依次赋给i
print(f"adding {i} to the list,")
elements.append(i)
for i in elements:
print(f"element was: {i}")
range(start, end, scan):
参数含义:
start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);
end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
在进行for循环时变量由for循环自动创建,列表的复制 a = the_count[:],range()函数是创建一个数值范围,注意range(0,6)指的是0,1,2,3,4,5
element.append(i)追加i到空列表element里面
上一篇: python第八课