zoukankan      html  css  js  c++  java
  • 笨办法学Python(learn python the hard way)--练习程序11-20

    #ex11.py
    1
    print("How old are you?",end=''), 2 age = input() 3 print("How tall are you?",end=''), 4 height = input() 5 print("How much do you weigh?",end=''), 6 weight = input() 7 8 print(f"So,you're {age} old,{height} tall and {weight} heavy.")
    #ex12.py
    1
    age = input("How old are you?") 2 height = input("How tall are you?") 3 weight = input("How much do you weigh?") 4 5 print(f"So,you're {age} old,{height} tall and {weight} heavy.")
    #ex13.py
    1
    from sys import argv 2 3 script, first, second, third = argv 4 a=input("what's your name?") 5 print(a) 6 print("The script is called:", script) 7 print("Your first variable is:", first) 8 print("Your second variable is:", second) 9 print("Your third variable is:", third)
     #ex14.py
    1
    from sys import argv 2 3 script, user_name = argv 4 prompt = '> ' 5 6 print("Hi %s, I'm the %s script."%(user_name, script)) 7 print("I'd like to ask you a few questions.") 8 print("Do you like me %s?"%user_name) 9 likes = input(prompt) 10 11 print("Where do you live %s?"%user_name) 12 lives = input(prompt) 13 14 print("What kind of computer do you have?") 15 computer = input(prompt) 16 17 print(""" 18 Alright,so you said %r about liking me. 19 You live in %r. Not sure where that is. 20 And you have a %r computer. Nice. 21 """%(likes, lives, computer))
     #ex15.py
    1
    from sys import argv 2 3 script, filename = argv 4 5 txt = open(filename) 6 7 print("Here's your file %r:"%filename) 8 print(txt.read()) 9 txt.close() 10 11 print("Type the filename again:") 12 file_again = input("> ") 13 14 txt_again = open(file_again) 15 16 print(txt_again.read()) 17 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 2 3 script, filename = argv 4 5 print("We're going to erase %r."%filename) 6 print("If you don't want that, hit CTRL-C(^C).") 7 print("If you do want that, hit RETURN.") 8 9 #input作用是中断程序,选择是否要删除文件内容 10 input("?") 11 12 print("Opening the file...") 13 #'w'是open的参数,默认是读,只有特别指定才可以进入写操作,写操作是先删除再新建,所以后面的truncate实际是不起作用的 14 target = open(filename, 'w') 15 16 print("Truncating the file. Goodbye!") 17 target.truncate() 18 19 print("Now I'm going to ask you for three lines.") 20 21 line1 = input("line 1: ") 22 line2 = input("line 2: ") 23 line3 = input("line 3: ") 24 25 print("I'm going to write these to the file.") 26 27 target.write(line1) 28 target.write(" ") 29 target.write(line2) 30 target.write(" ") 31 target.write(line3) 32 target.write(" ") 33 34 print("And finally, we close it.") 35 target.close()
     #ex17.py
    1
    from sys import argv 2 from os.path import exists 3 4 script, from_file, to_file = argv 5 6 print("Copying from %s to %s" %(from_file, to_file)) 7 8 # we could do these two on one line too, how? 9 #indata = open(from_file).read() 10 input = open(from_file) 11 indata = input.read() 12 13 print("The input file is %d bytes long"%len(indata)) 14 15 print("Does the output file exist?%r" %exists(to_file)) 16 print("Ready, hit RETURN to continue, CTRL-C to abort.") 17 input("??") 18 19 output = open(to_file, 'w') 20 output.write(indata) 21 print("Alright,all done.") 22 23 output.close() 24 input.close()
     #ex18.py
    1
    # this one is like your scripts with argv 2 def print_two(*args): 3 arg1, arg2 = args 4 print("arg1: %r, arg2: %r"%(arg1, arg2)) 5 6 # ok, that *args is actually pointless, we can just do this 7 def print_two_again(arg1, arg2): 8 print("arg1: %r, arg2: %r"%(arg1, arg2)) 9 10 # this just takes one argument 11 def print_one(arg1): 12 print("arg1: %r"% arg1) 13 14 # this one takes no arguments 15 def print_none(): 16 print("I got nothin'.") 17 18 19 print_two("Zed","Shaw") 20 print_two_again("Zed","Shaw") 21 print_one("Frist!") 22 print_none()
     #ex19.py
    1
    # 函数里边的变量和脚本里边的变量之间是没有连接的 2 def cheese_and_crackers(cheese_count, boxes_of_crackers): 3 print("You have %d cheeses!"%cheese_count) 4 print("You have %d boxes of crackers!"%boxes_of_crackers) 5 print("Man that's enough for a party!") 6 print("Get a blanket. ") 7 8 9 print("We can just give the function numbers directly:") 10 cheese_and_crackers(20,30) 11 12 13 print("OR, we can use variables from our script:") 14 amount_of_cheese = 10 15 amount_of_crackers = 50 16 17 cheese_and_crackers(amount_of_cheese, amount_of_crackers) 18 19 20 print("We can even do math inside too:") 21 cheese_and_crackers(10+20,5+6) 22 23 24 print("And we can combine the two, variables and math:") 25 cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
     #ex20.py
    1
    #函数和文件如何一起协作 2 from sys import argv 3 script, input_file = argv 4 5 #定义函数:将读到的内容打印出来 6 def print_all(f): 7 print(f.read()) 8 9 #定义函数:将光标定位到起始位置 10 def rewind(f): 11 f.seek(0) 12 13 #定义函数: 将读到的行数及该行内容打印出来 14 def print_a_line(line_count, f): 15 print(line_count, f.readline()) 16 17 #打开文件 18 current_file = open(input_file) 19 20 print("First let's print the whole file: ") 21 22 print_all(current_file) 23 24 print("Now let's rewind, kind of like a tape.") 25 26 rewind(current_file) 27 28 print("Let's print three lines:") 29 30 #current_line = 0 31 #current_line += 1 32 current_line = 1 33 print_a_line(current_line, current_file) 34 35 current_line = current_line + 1 36 print_a_line(current_line, current_file) 37 38 current_line = current_line + 1 39 print_a_line(current_line, current_file)
  • 相关阅读:
    Android开发_Animation
    spring开发_JDBC操作MySQL数据库_使用xml配置事务管理
    spring开发_AOP_代理模式
    java file 文件操作 operate file of java
    spring开发_spring构造注入Bean
    spring开发_spring中Bean的作用域_singleton_prototype
    spring开发_JDBC操作MySQL数据库
    java的jxl技术导入Excel
    spring开发_spring环境搭建
    魅族m8开发 step by step(1)(让程序跑起来)
  • 原文地址:https://www.cnblogs.com/xiyouzhi/p/9600440.html
Copyright © 2011-2022 走看看