#ex11.py
1 print("How old are you?",end=''),
age = input()
print("How tall are you?",end=''),
height = input()
print("How much do you weigh?",end=''),
weight = input() print(f"So,you're {age} old,{height} tall and {weight} heavy.")
#ex12.py
1 age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?") print(f"So,you're {age} old,{height} tall and {weight} heavy.")
#ex13.py
1 from sys import argv script, first, second, third = argv
a=input("what's your name?")
print(a)
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
 #ex14.py
1 from sys import argv script, user_name = argv
prompt = '> ' print("Hi %s, I'm the %s script."%(user_name, script))
print("I'd like to ask you a few questions.")
print("Do you like me %s?"%user_name)
likes = input(prompt) print("Where do you live %s?"%user_name)
lives = input(prompt) print("What kind of computer do you have?")
computer = 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.py
1 from sys import argv script, filename = argv txt = open(filename) print("Here's your file %r:"%filename)
print(txt.read())
txt.close() print("Type the filename again:")
file_again = input("> ") txt_again = open(file_again) print(txt_again.read())
txt_again.close()

附ex15_sample:This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

 #ex16.py
1 from sys import argv script, filename = argv print("We're going to erase %r."%filename)
print("If you don't want that, hit CTRL-C(^C).")
print("If you do want that, hit RETURN.") #input作用是中断程序,选择是否要删除文件内容
input("?") print("Opening the file...")
#'w'是open的参数,默认是读,只有特别指定才可以进入写操作,写操作是先删除再新建,所以后面的truncate实际是不起作用的
target = open(filename, 'w') print("Truncating the file. Goodbye!")
target.truncate() print("Now I'm going to ask you for three lines.") line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ") print("I'm going to write these to the file.") target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n") print("And finally, we close it.")
target.close()
 #ex17.py
1 from sys import argv
from os.path import exists script, 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?
#indata = open(from_file).read()
input = open(from_file)
indata = input.read() print("The input file is %d bytes long"%len(indata)) print("Does the output file exist?%r" %exists(to_file))
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input("??") output = open(to_file, 'w')
output.write(indata)
print("Alright,all done.") output.close()
input.close()
 #ex18.py
1 # this one is like your scripts with argv
def print_two(*args):
arg1, arg2 = args
print("arg1: %r, arg2: %r"%(arg1, arg2)) # 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 arguments
def print_none():
print("I got nothin'.") print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("Frist!")
print_none()
 #ex19.py
1 # 函数里边的变量和脚本里边的变量之间是没有连接的
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print("You have %d cheeses!"%cheese_count)
print("You have %d boxes of crackers!"%boxes_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 from our script:")
amount_of_cheese = 10
amount_of_crackers = 50 cheese_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.py
1 #函数和文件如何一起协作
from sys import argv
script, input_file = argv #定义函数:将读到的内容打印出来
def print_all(f):
print(f.read()) #定义函数:将光标定位到起始位置
def rewind(f):
f.seek(0) #定义函数: 将读到的行数及该行内容打印出来
def print_a_line(line_count, f):
print(line_count, f.readline()) #打开文件
current_file = open(input_file) print("First let's print the whole file:\n") print_all(current_file) print("Now let's rewind, kind of like a tape.") rewind(current_file) print("Let's print three lines:") #current_line = 0
#current_line += 1
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)

笨办法学Python(learn python the hard way)--练习程序11-20的更多相关文章

  1. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  2. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  3. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  4. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  5. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  6. 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘

    笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln  怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...

  7. 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍

    点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...

  8. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

  9. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

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

  10. 《笨办法学Python 3》python入门书籍推荐|附下载方式

    <笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...

随机推荐

  1. Nginx基本属性配置

    Nginx基本属性配置 1.找到安装目录下conf 文件下的nginx.conf文件 通过 Notepad++打开进行 属性配置   image ==>   image 2.worker_pro ...

  2. idea的热部署

    1:先找到你要热部署的tomcat之后 ,在设置tomcat时  先选择 server,里面有On 'Update' action ()  和 On frame deactivation 这两项  都 ...

  3. ecshop注册用户增加手机验证功能

    1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name="username" type="text" s ...

  4. Vue 基础 day02

    Vue Devtools 安装 https://chrome.google.com/webstore/search/vue%20devtools?hl=zh-CN 需要翻墙 过滤器 概念: Vue.j ...

  5. zend studio远程自动上传代码并执行

    http://devzc.com/archives/382/zend_studio_sftp_upload_and_exec/ 最近要做服务的接口测试,公司原有的ide测试工具对于数组的参数化很弱.由 ...

  6. 自己挖的坑自己填--JVM报内存溢出

    在写定时任务时,对表数据进行批量操作,测试数据有10万条左右,在测试时发现跑着跑着出现内存溢出现象,最后发现创建的对象paramList 和tmBeanList没有被回收,经过资料查找,发现是循环内不 ...

  7. 20171110面试笔记 服务器端程序员+C/C++开发

    socket 模型: https://www.cnblogs.com/nsnow/archive/2011/05/03/2036017.html http://blog.csdn.net/normal ...

  8. 循环结构 :do-while

    循环结构 :do-while 循环四要素: 1.初始化条件 2.循环条件 3.循环体 4.迭代条件 格式: 1.初始化条件 do{ 3.循环体 4.迭代条件 }while(2.循环条件); publi ...

  9. VBA中如何用environ$ 或 environ方法取得环境变量?

    用索引号取得环境变量Sub EnumSEVars() Dim strVar As String Dim i As Long strVar = Environ$(i) & Then Exit F ...

  10. 正在连接localhost...无法打开到主机的连接。 在port 8080: 连接失败

      在cmd中用telnet连接tomcat,出现了"正在连接localhost...无法打开到主机的连接. 在port 8080: 连接失败"原因是我的tomcat是绿色版的,没 ...