笨办法学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入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- Postman Tests脚本的使用
直接在Tests中写js代码断言结果,Test Result展示运行结果,简单方便. 示例脚本: var jsonData = JSON.parse(responseBody); var num = ...
- layui的分页使用(前端分页)
<div id="one"></div>//显示数据库中数据的<ul id="ones"></ul>//显示分页 ...
- Windows DiskPart
win+r打开运行窗口,输入diskpart命令,按回车键或点击确定按钮即可打开如下所示界面: 输入help可以打印帮助信息 List Disk:显示本机的所有磁盘,以便正确操作目标磁盘 Select ...
- 解决django项目无法连接远程mysql的问题
我们都知道django项目可以通过修改settings.py文件中的DATABASES这个对象,使用不同的数据库. 如图所示,我们想连接远程的mysql,修改settings.py的配置 然后我们在终 ...
- oracle--少见操作、如何调整dos窗口大小、字符集设置
如何调整dos窗口大小 1.set linesize 400; 2.右键 --默认值 断开.连接 disconn ; conn ww/ww; 关于字符集 操作系统环境变量针对语言项设置有几个,我经常设 ...
- mysql树查询、递归查询
关键词:mysql树查询,mysql递归查询 转自:http://www.cnblogs.com/c-h-y/p/9420726.html 之前一直用的是Oracle,对于树形查询可以使用start ...
- jdk与eclipse安装注意事项
1.安装这两个软件,一定要注意使得两个软件的版本是一致的: 1.1编写小程序测试jdk的版本号: public class Text{ public static void main(String[] ...
- JDK11 | 第五篇 : 启动单个Java源代码文件的程序
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/h1L4FmzVSix434gVt8Fc7w 一.简介 JEP330-启动单文件代码程序(L ...
- Spring Cloud部署+Mybatis整合
一:架构简介 Spring Cloud是微服务思想的体现.每个项目单独部署,我只需要知道你服务的name就能直接调用你,而不关心你的ip和端口的变化.当接口服务不可用的时候,我能感知到你无法用了,就不 ...
- APMServ升级PHP至5.3
APMServ5.2.6 的php版本是php5.2.6,所以需要升级一下PHP版本:1.到 php下载地址下载PHP5.3的VC6版本的zip文件,我下载的是:php-5.3.23-Win32-VC ...