zoukankan      html  css  js  c++  java
  • ex19-20 函数、变量和文件

    ex19

    函数中的全局变量和脚本中的变量没有联系,函数中的变量可以通过多种方式赋予。

     1 #-*- coding: UTF-8 -*- 
     2 def cheese_and_crackers(cheese_count,boxes_of_crackers):
     3   print "You have %d cheese!" % 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   #创建函数:设置两个参数;四个包含了参数的print。
     8   
     9   
    10 print "We can just give the function number directly:"
    11 cheese_and_crackers(20,30) #直接对函数中的参数赋值
    12 
    13 print "OR,we can use variables from our script."
    14 amount_of_cheese = 10
    15 amount_of_crackere = 5
    16 #要查看变量类型,可以输入type(amount_of_cheese)
    17 cheese_and_crackers(amount_of_cheese,amount_of_crackere)#通过脚本中的变量来对函数的参数赋值
    18 
    19 print "We can even do math inside too."
    20 cheese_and_crackers (10+20,5+6)#在函数内进行运算
    21 
    22 
    23 print "And we can combine the two,variable and the math."
    24 cheese_and_crackers(amount_of_cheese+10,amount_of_crackere+1000)#在函数内进行运算,并通过脚本中的变量进行
    25 #发现一个特点,创建的这个函数,不仅仅是包括参数,还包括下边二级代码的那一系列指令。

    附加练习:自己编写一个函数,并至少用10种方式运行(我只用了3种 ==)

     1 #-*- coding: UTF-8 -*- 
     2 def shopping_list (what_I_want,price):#老是会忘记冒号,要经常提醒自己。
     3    print "So I wanna %s And its price is %d RMB"% (what_I_want,price)#前面设置两种不同的格式化字符,后边也可以给放到一个括号里边)
     4    print "Fine,let us just take it."
     5    
     6 print "Now I gonna go shopping." 
     7 shopping_list ("Juice" ,10)
     8 
     9 print "Now I want two kinds of beer."
    10 shopping_list("IPA"+"	Lager",10+15)#如果想要	起到空格的作用,那么就需要在前面设置为%s
    11 
    12 print "OK i also wanna something for dinner."
    13 dish = "pork"+"&eggs"
    14 the_price = 30+10#要非常注意,脚本中的变量和函数中的全局变量不要设置成相同
    15 shopping_list(dish,the_price)
    16 
    17 print "HAHA man you look just like a idiot."

    ex20

     1 #-*- coding: UTF-8 -*- 
     2 from sys import argv
     3 
     4 script,input_file = argv
     5 
     6 def print_all(f):#创建函数,读取并打印文件内容
     7  print f.read()#后边如果不加(),就没法读,或者读了也没法打印
     8 
     9 def rewind (f):#创建函数,把文件指针设置到从头开始
    10  print f.seek(0)
    11  
    12 def print_a_line(line_count,f):#创建函数,按照行来读取并打印文件
    13  print line_count,f.readline()
    14  
    15 current_file = open(input_file)#打开文件
    16 
    17 print"First lets read the whole file.
    "
    18 
    19 print_all (current_file) #一开始还疑惑是否可以直接用 Input_file ,后来发现自己傻了,肯定要先打开啊~~
    20 
    21 print "Now let's rewind,kind of like a tape."#不是很明白这里rewind的意义,难道在进行上一步之后指针到了最后了?
    22 
    23 rewind(current_file)
    24 
    25 print "Let's print three lines."
    26 #逐行读取并打印
    27 current_line = 1
    28 print_a_line(current_line,current_file)
    29 
    30 current_line = current_line + 1 #也可以写成current_line += 1
    31 print_a_line(current_line,current_file)
    32 
    33 current_line = current_line + 1
    34 print_a_line(current_line,current_file)
    35 
    36 '''
    37 file.seek()方法标准格式是:seek(offset,[whence=0])offset:相对于whence的偏移量,字节数,可以为负数,
    38 whence省略时,默认从文件开头算起
    39 whence:给offset参数的偏移参考,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起
    40 '''
  • 相关阅读:
    实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序
    hibernate官方新手教程 (转载)
    C++ 清空消息队列
    java中接口的定义与实现
    Scrum 学习笔记
    Ubuntu中全然卸载Nginx
    ScrollView 在嵌套 ViewPager 时出现的问题
    Java的递归算法
    Android GPS获取当前经纬度坐标
    【数据结构】——排序算法——1.1、直接插入排序
  • 原文地址:https://www.cnblogs.com/dingtou00/p/7707297.html
Copyright © 2011-2022 走看看