程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

模块(0)

标准库(0)

标签  

标准库模块(0)

字符串(0)

日期归档  

第七天学习 笨办法学python3 习题16-习题19

发布于2019-08-22 16:51     阅读(1502)     评论(0)     点赞(5)     收藏(4)


习题16(文件写入)

输入以下代码

from sys import argv

script,filename=argv

print("We're going to crase %r."%filename)
print ("If you didn't want that,hit CTRL-C (^C).")
print("If you do want that,hit RETURN.")

input("?")

print ("Opening the file...")
target=open(filename,'w')

print("Truncating the file Goodbye!")
target.truncate()

print("Now I'm going to ask for you three lines.")

line1=input("line1: ")
line2=input("line2: ")
line3=input("line3: ")

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

下面是这段代码的解析

from sys import argv

script,filename=argv

print("We're going to crase %r."%filename)
print ("If you didn't want that,hit CTRL-C (^C).")
print("If you do want that,hit RETURN.")  #这里出现了一个选择 回车继续  ctrl-c取消

input("?")

print ("Opening the file...")   #打开指定文件的说明
target=open(filename,'w')   #这里是之前的open的一种作用了  覆盖写入文件  open(文件名,'W')

print("Truncating the file Goodbye!")
target.truncate()   #truncate作用是裁断文件大小 括号中无数据则表示默认 相当于把源文件清空

print("Now I'm going to ask for you three lines.")

line1=input("line1: ")
line2=input("line2: ")
line3=input("line3: ")

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()   #关闭 结束这个函数一切操作
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

输出结果如图
在这里插入图片描述
来检验一下daft.txt
在这里插入图片描述

习题17(文件间的复制)

输入以下代码

from sys import argv
from os.path import exists

script,from_flie,to_file=argv  #解包的分别程序名称,是源自哪一个文件,目的文件

print("Copying from %s to %s"%(from_flie,to_file))

in_flie=open(from_flie)
indata=in_flie.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,CTAL-CTAL-C to abort.")
input()

out_file=open(to_file,'w')
out_file.write(indata)

print("Alright,all done.")

out_file.close()
in_flie.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

本代码实现把一个的内容代替另一个文件(把1.txt的内容代替到2.txt)
结果如图

在这里插入图片描述
在看一2.txt的结果(我将1.txt设置成11111 2.txt内容设置成22222)
在这里插入图片描述

习题18(定义函数)

输入以下代码

def print_two(*args):
  arg1,arg2=args
  print("arg1:%r,arg2:%r"%(arg1,arg2))
  
def print_two_again(arg1,arg2):
  print("arg1:%r.arg2:%r"%(arg1,arg2))
  
def print_one(arg1):
  print("arg1:%r"%arg1)
  
def print_none():
  print("I got nothin'.")
  
print_two("He","Rui")
print_two_again("He","Rui")
print_one("First!")
print_none()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

def作用:定义功能函数 在得分函数声明以及函数名称后的变量名称(括号中内容)不必连在一起
args:允许函数接收多个参数
之前输出定义结果的方法,有直接输出;有先在语句中定义类型在同一个print中,在输出内容后%打上输出内容;还有先定义import后,在控制台输入内容实现输出,而本次是实现先定义函数,最后在结尾声明函数内容,注意,结尾对函数内容的填写,每一个变量要用省略号括起来,与前面函数声明的量相同。

习题19(定义函数与运算)

输入以下代码

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在这里插入图片描述
这里先要说明一个东西,在python中对书写格式有着严格的要求,比如每一段平行的内容所顶格的数相同,在def定义功能函数外(除了实现对其变量值的输入)的函数要顶格写,比如在这一段中后面的输出,必须顶格写。
还有一个是输出是安装输入顺序来的,在这一段程序中

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

def是在最后有关cheese_and_ceacker()中变量的输出中财结束的,所以先输出print(“We can just give the function numbers directly:”),之后输出def函数中内容。
后面的一段中对于cheese_and_crackers()中变量重新声明,由于已经定义cheese_and_crackers,不用重新定义,且后面的输出会循照def cheese_and_crackers()来进行输出。
在最后一段中,print(“And we can combine the two, variables and math:”)
cheese_and_crackers(amount_of_cheese + 100,
amount_of_crackers + 1000)是在对上面的函数变量进行回溯,而第二部分已经重新声明变量了,所以对重新声明的变量进行运算。而此时第三部分的变量并没有重新对于函数的变量名称进行定义,是对第二部分的函数,也就是与第四部分的变量名称相同的进行运算。



所属网站分类: 技术文章 > 博客

作者:追梦骚年

链接:https://www.pythonheidong.com/blog/article/53058/0f8168e812b11a21a42e/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)