ex11

# -*- coding: utf-8 -*-
print "How old are you?" # add ',(comma)' 防止输出新行,结束此行
age = raw_input()
print "How tall are you?",
height = raw_input() # 接受,而input(),把输入 sth 当python 代码处理
print "How much do you weigh?",
weight = raw_input()
print "So,you're %r old ,%r tall and %r heavy." %(age ,height, weight)x = int(7.8) #  int()转换成整数
print x ,
a = int(float(raw_input())),
print a
w = input(),# 输入除纯数字字符串之前加u,使用%s 正常
print w
# raw_input 将所有输入作为字符串看待,返回字符串类型。
# input 只能接受合法的python表达式。

ex12

# -*- coding: utf-8 -*-
age = raw_input("How old are you?") # 添加文字
height = raw_input("How tall are you?")
weight = raw_input("How much do you weigh?")
print "So , you're %r old, %r tall and %r heavy." %(age, height, weight )

ex13

# -*- coding: utf-8 -*-
from sys import argv # import(输入)是将python的功能引入脚本的办法,
# argv (是argument variable) 参数变量
script, first, second, third = argv
# 将argv解包,将所有参数放到同一个变量下,必须输入三个变量,先写script(脚本),
print "The script is called:", script
print "Your first variable is:",first
print "Your second variable is:",second
print "Your third variable is:",third
#当运行时提供的参数个数不对时,错误信息,‘need more than xx values to unpack.’

ex14

# -*- coding: utf-8 -*-
from sys import argv
script, user_name, age = argv
prompt = '>' # use '>' as a 提示符
print "Hi %s, and you're %r, I'm the %s script." % (user_name, age,script)
print "I'd like to ask you a few questions."
print "Do you like me %s ?"% user_name
likes = raw_input(prompt)print "Where do you live %s?" %user_name
lives = raw_input(prompt)print "What kind of computer do you have?"
computer = raw_input(prompt)print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" %(likes, lives, computer)

ex15

# -*- coding: utf-8 -*-
from sys import argv
script, filename = argvtxt = open(filename) # 获得文件名,需要在同一个文件里,return a file object.print "Here's your file %r:" % filename
print txt.read() # read 读取文本内容print "Type the filename again:"
file_again = raw_input('<')txt_again = open(file_again)print txt_again.read()

ex16

# -*- coding: utf-8 -*-
from sys import argvscript, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you want that, hit REATURN."raw_input("?")
print "Opening the file..."
target = open(filename,'w') # 用“w" 模式打开, w ,write写入模式, r , read 读取模式,
# a ,append追加模式, "w+""r+""a+"同时读写打开, 直接open(filename) in r 这是默认工作方式。print "Truncating the file. Goodbye!"
target.truncate() # xx.turncate 清空文件print "Now I'm going to ask you for three lines."line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")print "I'm going to write these to the file."target.write(line1) # write写入
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")print "And finally, we close it."target.close()

ex17

# coding= utf-8
from sys import argv
from os.path import existsscript, from_file, to_file = argv# 输入的文件名若不存在将创建一个新的print "Copying from %s to %s." % (from_file, to_file)#We could do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read() #将文件数据存在indataprint "The input file is %d bytes long" % len(indata)# len 字节长度print "Does the output file exist?%r" %exists(to_file)# exists 将文件名字符串作为参数,
#文件存在返回True , 不存在返回Falseprint "Ready, hit RETRUN to countiue, CTRL-C to abort."
raw_input()out_file = open(to_file, "w")
out_file.write(indata)print "Alright, all done."out_file.close()
in_file.close()

ex18

# -*- coding: utf-8 -*-# this is like you scripts with argv
def print_two(*args):  # def is define. 'print_two' is name of function.arg1, arg2 = args # ':' 用来结束本行,此代码用解压参数print "arg1: %r, arg2: %r" % (arg1,arg2) # 5行"*" 作用把所有参数接受放到args 中去
# 函数名称可以是字母数字下划线,但不能是数字开头
# ok, that *args is actually pointless. we can just do this
def print_two_again(arg1, arg2):print "arg1: %r, arg2: %r" % (arg1, arg2) # 其实可以不用解压# this just takes one argument
def print_one(arg1):print "arg1: %r" % arg1 # this one takes no argument
def print_none():print "I got nothin'."print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

ex19

# -*- coding: utf-8 -*-def cheese_and_crackers(cheese_count, boxes_of_crackers):print "You haxe %d cheeses." % cheese_countprint "You have %d boxes of crackers!" % boxes_of_crackersprint "Man that's enough for a party!"print "Get a blanket. \n"print "we can just give the function numbers dirextly:"
cheese_and_crackers(20, 30) # 定义函数后,你可以直接输入数到函数中去。print "OR,we can use variables from 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 + 100) # 赋值的参数可以同直接
# 输入的数字做数学运算

ex20

# -*- coding: utf-8 -*-from sys import argv
script, input_file = argvdef print_all(f):print f.read()def rewind(f):f.seek(0) # 'f'只是一个变量名,f.seek(0) 每次运行它时就回到了文件的开始,0 byte ,即第一个byte的位置def print_a_line(line_count, f):print line_count, f.readline() # f.readline() 读取文件的一行,然后移到\n 后current_file = open(input_file)print "First let's print the whole file:\n", #  在print 语句后加, 就不会把他自己的\n 打印出来了
# readline 函数返回的内容中包含文件本来就有的\n,而 print 打印时又会添加 \n ,所以!
print_all(current_file)print "Now let'rewind, kind of like a tape."rewind(current_file) # 运行f.seek(0)回到0字节处print "Let's print three lines:"current_line = 1 # current_line 是独立变量需手动改值
print_a_line(current_line, current_file) current_line = current_line + 1
print current_line
print_a_line(current_line, current_file)current_line += 1#'+=' 是x = x + y 的简写 x += y
print current_line
print_a_line(current_line, current_file )

笨办法学python ex11-20相关推荐

  1. 笨办法学python 粗略笔记(learn python the hard way)

    笨办法学python 粗略笔记(learn python the hard way) 标签(空格分隔): python # _*_ coding: utf_8 _*_ ''' ### ex1 prin ...

  2. 计算机编程书籍-笨办法学Python 3:基础篇+进阶篇

    编辑推荐: 适读人群 :本书适合所有已经开始使用Python的技术人员,包括初级开发人员和已经升级到Python 3.6版本以上的经验丰富的Python程序员. "笨办法学"系列, ...

  3. python教程第四版pdf下载-笨办法学python第四版

    笨办法学python第四版是由Zed Shaw所编写的一本书.如果你还是Python新手,那么这是一本非常不错的入门书籍.书本里以习题方式,引导读者慢慢学会了编程. 目录: 习题 0: 准备工作 习题 ...

  4. python教程第四版pdf下载-笨办法学python第四版 电子书(pdf格式)

    笨办法学python第四版是由Zed Shaw所编写的一本书.如果你还是Python新手,那么这是一本非常不错的入门书籍.书本里以习题方式,引导读者慢慢学会了编程. 目录: 习题 0: 准备工作 习题 ...

  5. python教程第四版pdf下载-笨办法学python 第四版 中文pdf高清版

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  6. 笨办法学 Python · 续 练习 33:解析器

    练习 33:解析器 原文:Exercise 33: Parsers 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 想象一下,你将获得一个巨大的数字列表,你必须将其输入到电子表格 ...

  7. 笨办法学python pdf 第三版_笨办法学python第三版

    笨办法学python第三版pdf电子书是一本Python学习参考书,是美国程序员Zed A.Shaw编著,通过简单通俗的方法,结合内部的集体,让程序员学懂python,适用于初级学习python编程的 ...

  8. 《笨办法学Python》——习题3

    文章目录 基本习题 1. 完成基本习题 加分习题 1. 使用#在代码每一行的前一行为自己写一个注解,说明一下这一行 2. 记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行 ...

  9. 笔记37 笨办法学python练习43面向对象OOP的游戏代码(二)代码的反复理解

    笔记37 笨办法学python练习43面向对象OOP的游戏代码(二)代码的反复理解 连续贴着这个练习43的代码折腾了整整两天,把那些英文文本翻译为中文文本,重新装进这个代码之中.本想一段一段的运行,发 ...

  10. 笨办法学python在线阅读_笨办法学python全集.pdf

    TableofContents 笨办法学Python 1.1 序言 1.2 前言 1.3 简介 1.4 练习0.安装和准备 1.5 练习1.第一个程序 1.6 练习2.注释和井号"#&quo ...

最新文章

  1. Lyft估值目标近200亿美元 有望成今年来美国最大IPO
  2. idea getset的快捷键
  3. Linux下对网络进行配置nmcli、nmtui
  4. php 点击文本框弹出时间,点击Input框弹出日期选项
  5. LINQ-to-SQL那点事~LINQ-to-SQL中的并发冲突与应对
  6. 关闭或修改 IIS 443 端口
  7. HTTP协议与HTTPS的区别
  8. 图层重命名快捷键_玩转CAD快捷键(大全),一篇文章就够了
  9. oracle 没有debug权限,开启Oracle的debug级别日志
  10. a^x ≡1(mod n) Ord_n(a)=x什么意思
  11. jsp el表达式无法正常显示解决方法
  12. 使用Rider和Emmylua进行UnLua配置
  13. 用正则表达式验证邮箱、密码、QQ号、手机号、身份证号
  14. Go语言(Golang)的Web框架比较:gin VS echo
  15. 将uiimageview设置成纯圆形
  16. Avalonia UI 简介
  17. NIO与JVM基本概念
  18. SQLPro Studio for Mac(可视化数据库管理工具)
  19. Android输入法弹窗导致背景变形处理
  20. 数据库系统原理-课程目标

热门文章

  1. matlab图像学习笔记
  2. 第1章-数据探索(1)-数据预处理
  3. 移动硬盘提示需要格式化
  4. Java笔记(学习中。。)
  5. python画函数曲线-2.3python如何绘制二次函数图像
  6. 苹果第四财季净利107.14亿美元;美团关闭共享充电宝业务;支付宝上线iPhone X碎屏险丨价值早报
  7. 基于JAVA的疫情防控核酸检查管理系统【数据库设计、源码、开题报告】
  8. HTML5挂号,基于SSH的医院在线挂号
  9. kali/debian/snap-store debain/linux的软件商店下载
  10. python numba_用 Numba 加速你的 Python 代码,性能轻松大提升