笨办法学python ex11-20

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 = argv

txt = 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 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 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 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?
in_file = open(from_file)
indata = in_file.read() #将文件数据存在indata

print "The input file is %d bytes long" % len(indata)# len 字节长度

print "Does the output file exist?%r" %exists(to_file)# exists 将文件名字符串作为参数,
#文件存在返回True , 不存在返回False

print "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_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 dirextly:"
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 + 100) # 赋值的参数可以同直接
# 输入的数字做数学运算

ex20

# -*- coding: utf-8 -*-

from sys import argv
script, input_file = argv

def 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 )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值