笨办法学Python(learn python the hard way)--练习程序11-20
#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的更多相关文章
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- C#7.0新特性和语法糖详解
转自IT之家网--DotNet码农:https://www.ithome.com/html/win10/305148.htm 伴随Visual Studio 2017的发布,C#7.0开始正式走上工作 ...
- Flask 启动配置
数据迁移.
- [Web 前端] 010 css 常用的边框设置
css 常用边框属性 概览 参数 释义 border u设置边框属性(可以多个) border-color 边框颜色 border-style 边框样式solid 实线,dotted 点状线,dash ...
- mysql忘记密码/修改密码
关键词:忘记密码,修改密码,mysql忘记密码,mysql修改密码 转自:https://www.cnblogs.com/jdxn/p/6847089.html 方法1: 用SET PASSWORD命 ...
- ML5238电池管理芯片笔记
根据公司需要开发了以ML5238电池管理芯片+STM8S为核心的电池管理系统.由于前期对BMS系统还是了解甚少,开发起来也遇到了不少困难.再开发管理系统的同时,我也开发了管理系统的上位机, ...
- Python2中range 和xrange的区别??
两者用法相同,不同的是range返回的结果是一个列表,而xrange的结果是一个生成器, 前者是直接开辟一块内存空间来保存列表,后者是边循环边使用,只有使用时才会开辟内存空间, 所以当列表很长时,使用 ...
- log.info()传入多个参数的方法
不知道项目里用的是啥 ** 版本的 log4j 居然不能传入变长参数 logger.info(String.format("%s %s %s", username, feature ...
- PS使用记录:人像(证件照)更换背景
PS使用记录:人像(证件照)更换背景 参考:非常干净的抠羽毛ps教程抠图羽毛 (1)准备2019PS,原图 (2)选择人像:选择->主体 (3)边缘处理:选择 ->选择并遮住 ->选 ...
- 剑指offer--字符串
C/C++中每个字符串都以字符'\0'作为结尾,这样我们就可以很方便的找到字符串最后的尾部.由于这个特点,每个字符串中都有一个额外字符的开销,稍不留神就会造成字符串的越界. 为了节省内存,C/C++把 ...
- 基于谷歌开源的TensorFlow Object Detection API视频物体识别系统搭建自己的应用(四)
本章主要内容是利用mqtt.多线程.队列实现模型一次加载,批量图片识别分类功能 目录结构如下: mqtt连接及多线程队列管理 MqttManager.py # -*- coding:utf8 -*- ...