Lear python the hard way ex19-ex20
ex19:
#ex19 函数里边的变量和脚本里的变量之间是没有连接的def cheese_and_crackers (cheese_count, box_of_crackers):print ("You have %d cheeses!" %cheese_count)print ("You have %d boxes of crackers!" %box_of_crackers)print ("Man that's enough for a party!")print ("Get a blanket.\n")print ("We can just give the function numbers directly.")
cheese_and_crackers(20,30)print ("Or, we can use variables form our script:")
amount_of_cheese =10
amount_of_crackers =50cheese_and_crackers(amount_of_cheese, amount_of_crackers)print("We can even do math inside too:")
cheese_and_crackers(10+20, 5+6)print ("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese +100, amount_of_crackers+1000)
ex20:
from sys import argvscript, input_file =argvdef print_all(f):print (f.read()) #定义“print_all”这个函数,这个函数有一个变量,函数的作用是打印出名称为变量名的文件的内容def rewind(f):f.seek(0) #重新设置文件读取指针到开头def print_a_line(line_count, f):print (line_count, f.readline()) #用于打印从文件中读取的行数;readline()用于从文件读取整行,括号中若定义了数字即为从文件中读取的字节数current_file=open(input_file)print("First let's print the whole file:\n")print_all(current_file)rewind(current_file)print("Let's print three lines:")
current_line=1
print_a_line(current_line,current_file)current_line=current_line+1
print_a_line(current_line,current_file)current_line=current_line+1
print_a_line(current_line,current_file)
Lear python the hard way ex19-ex20相关推荐
- python地板除与除法的区别_“地板除”还是“取整除”?地板除和天花板除floor division和ceil devision...
上一篇文章知识点太多,我需要一一查看理解. 在运算符 ➡ 算数运算符 ➡ 操作符 ➡ // 整除(地板除) 3//4 为什么叫地板除? 向下取整除,就是地板除 floor division 向上取整除 ...
- Python编程:orm之sqlalchemy模块
orm英文全称object relational mapping,对象映射关系 http://www.sqlalchemy.org/ 常用操作 """ MySQL-Pyt ...
- 笨方法学python: ex20, 函数和文件
练习目的: 在函数中包括对文件的操作 转载自: http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html python中对文件 ...
- ex20——python文件读read()、readline()、readlines()对比
''' Open的current_file文件只是一个变量而已,不过在这里它指的是一个文件,python里的文件有一个读取数据的"磁头", 通过"磁头"操作文件 ...
- python学习日记_第九天(ex20~21)
L20 函数和文件 1.进一步学习文件读取,使用readline方法逐行读取,学习和使用seek控制指针指向文件中的指定位置(详见加分题seek备注.) #coding:utf-8 #习题 20: 函 ...
- 笨办法学python 粗略笔记(learn python the hard way)
笨办法学python 粗略笔记(learn python the hard way) 标签(空格分隔): python # _*_ coding: utf_8 _*_ ''' ### ex1 prin ...
- 笨办法学Python笔记2(ex18~ex40)
八 命名.变量.代码.函数 从本节开始 所有注意事项都以注释的形式显示 ex18 # -- coding: utf-8 -- def print_two(*args):#命名函数 括号中*的作用是以* ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学python ex11-20
ex11 # -*- coding: utf-8 -*- print "How old are you?" # add ',(comma)' 防止输出新行,结束此行 age = r ...
最新文章
- 广东省生态土壤所孙蔚旻团队FEMS: 砷锑污染土壤剖面的微生物世界
- Mysql系列(三)—— Mysql主从复制配置
- 【机器学习入门笔记5:OpenCV像素的基础知识】20190203
- opencv python cv2.threshold()函数报错 TypeError: Expected cv::UMat for argument 'mat'
- [Python图像处理] 三十五.OpenCV图像处理入门、算数逻辑运算与图像融合(推荐)
- .NET Core验证ASP.NET密码
- beaninfo详解源码解析 java_Java后端精选技术:源码解析Spring Cloud Zuul
- Codeblocks 中文乱码解决方法
- 漫游Kafka实战篇clientAPI
- Spark之键值RDD转换(转载)
- hadoop 注解之 interfaceAudience interfaceStability
- html 双 直线 公文,公文格式标准页面设置
- docker架构——第一本docker书
- IDEA 打包docker镜像
- BootStrap-datepicker日期插件
- 代码比较工具三款功能强大Beyond compare、DiffMerge、WinMerge
- 如何修复图片清晰度呢?这三款软件不要错过
- 幻想破灭!为何“每个儿童一台笔记本电脑”项目屡屡陷入困境?
- python—简单的数据抓取一(简单的文字数据抓取(认识cookie)、简单的图片抓取)
- 开心网外挂辅助营销推广工具2010新版!!!