博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《笨方法学Python》ex24-31
阅读量:6896 次
发布时间:2019-06-27

本文共 5194 字,大约阅读时间需要 17 分钟。

  hot3.png

ex24

快速打字和学习英语的重要性

ex25 更多更多的实践

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):	'''Print  the first word after poping it off.'''	word = words.pop(0)	print word	def print_last_word(words):	'''Prints the last word after poping 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):	'''Print 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 firsr and last one.'''	words = sort_sentence(sentence)	print_first_word(words)	print_last_word(words)

需要注意:def 变量 之后跟引号,然后下一行缩进。

import ex25 → ex25 作为一个模块出现不需要.py的后缀,在这个模块里定义的函数也可以直接调用出来

运行终端时出现的提示:

unexpected unindent : 缩进不正确

invalid syntax  :  语法错误  (可能是漏掉引号或者括号)

ex26 恭喜你,现在可以考试了!

import ex25def break_words(stuff):    """This function will break up words for us."""    words = stuff.split(' ')    return wordsdef 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 worddef print_last_word(words):    """Prints the last word after popping it off."""    word = words.pop(-1)    print worddef 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)print "Let's practice everything."print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'poem = """\tThe lovely worldwith logic so firmly plantedcannot discern \n the needs of lovenor comprehend passion from intuitionand requires an explantion\n\t\twhere there is none."""print "--------------"print poemprint "--------------"five = 10 - 2 + 3 - 5print "This should be five: %s" % fivedef secret_formula(started):	jelly_beans = started * 500	jars = jelly_beans / 1000	crates = jars / 100	return jelly_beans, jars, cratesstart_point = 10000beans, jars, crates = secret_formula(start_point)print "With a starting point of: %d" % start_pointprint "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)start_point = start_point / 10print "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_wordsprint_first_and_last(sentence)print_first_and_last_sorted(sentence)

主要的问题:

用到了ex25中的函数 没有import

漏引号、半个括号、关键词拼写错误

缩进错误

ex27 记住逻辑关系

逻辑术语、真值表、True、False

ex28 布尔表达式练习

"abc" == 1、"abc" == 0、"abc" == "bc"  都是 False

"abc" and "EFG" 返回 " abc",1 and 1 返回 1 。 因为都是返回两个被操作对象中的一个,而非他们的布尔表达式True或者False,而且是第一个操作数(abc)

短路逻辑:任何以 False 开头的 and 语句都是直接处理成 False,不会检查后面的语句, True、or 同理

ex29-30  if、else和if

多个 elif 块都是 True 时 Python 只会运行它遇到的是 True 的第一个块,所以只有第一个 True 的块会运行。

people = 30cars = 40buses = 15if 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 < cars:	print "Maybe we could take the buses."else:	print "We still can't decide."if people > buses:	print "Alright, let's just take the buses."else:	print "Fine, let's stay home then."

ex31 作出决定

print "You enter a dark room with two doors. Do you go through door #1 or #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. Bear runs away." %bear		elif door == "2" :	print "You stare into the endless abyss at Cthlhu's retina"	print "1. Blueberries."	print "2. Yellow jacket clothspins."	print "3. Understanding revolvers yelling melodies."		insanity = raw_input("> ")		if insanity == "1" or insanity == "2":		print "Your boby survives powered by a mind of jello. Good job!"	else:		print "The insanity rots your eyes into a pool of muck. Goof job!"		else:	print "Your stumble around and fall on a knife and die. Good job!"

注意:  if、elif、else 需要接引号, 然后转行缩进

怎么判断一个数出于某个值域中?

1,使用 1 < x < 10    2,用 x in range(1, 10) 

转载于:https://my.oschina.net/kgjc/blog/537883

你可能感兴趣的文章
Supervisor进程管理软件的安装与配置
查看>>
细谈 vue - transition 篇
查看>>
Ubuntn中获取仓库中的工具源码与构建
查看>>
Html Dom getElementsByClassName
查看>>
Android 中文 API ---- tabhost使用方法一(tabwidget+framlayout)
查看>>
Kubernetes生产环境经验告诉你如何实现蓝绿部署和负载均衡
查看>>
go 缓存机制
查看>>
P2P路由模式的概念和优势
查看>>
wangframe如何扩展?
查看>>
7.Spring Boot配置文件application.yml
查看>>
计算学校周次,亲测成功!
查看>>
Centos 7 可安装 mysql5.7
查看>>
雷神2—狂热视觉震撼
查看>>
node.js实现多图片上传
查看>>
could not bind to address 0.0.0.0:443 no listening sockets available, shutting d
查看>>
Node.js 开发相关
查看>>
JFinal源码--获取表单数据
查看>>
JSONP安全防范解决方案新思路
查看>>
Web 开发最有用的50款 jQuery 插件集锦——《综合篇》
查看>>
import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包
查看>>