笨办法学Python(learn python the hard way)--练习程序11-20
2024-08-31 08:13:21
#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)
最新文章
- 阿里云直播PHP SDK如何使用
- Fuel快速安装OpenStack
- 卸载移动硬盘出现 device is busy
- 浅析selenium的page object模式
- Tomcat下的一些配置
- [改善Java代码]不要让类型默默转换
- Linux 命令 - cat: 合并文件至标准输出
- Nexus搭建Maven服务器
- Python学习之四【变量】
- BZOJ 3529 数表(莫比乌斯反演)
- Java集合之ArrayList源码分析
- [转]Hooked on DTrace
- dotnet core error 0x80070057
- 样式(Style)和主题(Theme)资源——样式资源
- 【2017-02-26】String类、Math类、DateTime类
- 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
- C#zip压缩类
- 从头调试stm32 HID
- vue环境配置脚手架环境搭建vue工程目录
- Qt访问注册表并调用子进程