笨办法学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入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- Nginx 日志切割(Logrotate)
Logrotate 配置文件 # ls /etc/logrotate.* /etc/logrotate.conf /etc/logrotate.d: cups dracut fmdcn httpd i ...
- php-fpm启动不起来,php-fpm无法启动的一种情况
今天碰了一个很奇怪的问题,平时好好的php-fpm修改了一个参数后,突然启动不起来了,试着把参数还原.甚至用备份的配置文件还原都没办法启动php,而且不给任务启动错误的提示,纳闷!!!后来上网找了个资 ...
- jQuery Ajax方法调用 Asp.Net WebService、WebMethod 的详细实例代码
将以下html存为ws.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <scri ...
- OOM排除与JVM调优
仅先记录,后续整理 1. 常用命令: jstat gcutil jmap 2. 打印GC执行情况: 通过执行jinfo -flag +PrintGCDetails <pid>直接动态开启, ...
- AtCoder Beginner Contest 133 B - Good Distance
地址:https://atcoder.jp/contests/abc133/tasks/abc133_b 核心问题:判断一个浮点数开方是否为整数 ; double ans1=sqrt(ans); if ...
- 一个完整的http请求响应过程
一. HTTP请求和响应步骤 图片来自:理解Http请求与响应 以上完整表示了HTTP请求和响应的7个步骤,下面从TCP/IP协议模型的角度来理解HTTP请求和响应如何传递的. 二.TCP/IP协 ...
- 埃及分数问题(带乐观估计函数的迭代加深搜索算法-IDA*)
#10022. 「一本通 1.3 练习 1」埃及分数 [题目描述] 在古埃及,人们使用单位分数的和(形如 $\dfrac{1}{a}$ 的,$a$ 是自然数)表示一切有理数.如:$\dfrac{ ...
- asp.net Swiper 轮播动画
原文:https://blog.csdn.net/qq_39656138/article/details/90451289 官网:https://www.swiper.com.cn/api/index ...
- 如何设置 ComboBox 下拉列表的高度或间距
ComboBox 的下拉列表部分总是很挤,看起不舒服,但是设置了 ItemHeight 没用,怎么办呢? 首先设置一个较大的 ItemHeight 值,比如 20: 然后设置 ComboBox 的 D ...
- 第一个progressive web application,发车!
progressive web application是谷歌推出的一种渐进式web应用,通过利用service-worker等来达到类似于原生应用,而且在chrome浏览器还可以添加到主页,完全就和一 ...