python3 _笨方法学Python_日记_DAY4

Day4

  • 习题  19:  函数和变量

 1 def cheese_and_crackers(cheese_count, boxes_of_crackers):
 2     print("You have %d cheeses!" % cheese_count)
 3     print("You have %d boxes of crackers!" % boxes_of_crackers)
 4     print("Man that‘s enough for a party!")
 5     print("Get a blanket.\n")
 6
 7 print("We can just give the function numbers directly:")
 8 cheese_and_crackers(20,30)
 9
10 print("OR,we can use variables from our script:")
11 amount_of_cheese = 10
12 amount_of_crackers = 50
13
14 cheese_and_crackers(amount_of_cheese, amount_of_crackers)
15
16 print("We can even do math inside too:")
17 cheese_and_crackers(10+20, 5+6)
18
19 print("And we can combine the two, variables and math:")
20 cheese_and_crackers(amount_of_cheese + 100,
21                     amount_of_crackers + 1000)

结果:

We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that‘s enough for a party!
Get a blanket.

OR,we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that‘s enough for a party!
Get a blanket.

We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that‘s enough for a party!
Get a blanket.

And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that‘s enough for a party!
Get a blanket.

自己编一个函数:

1 def pingfang(x):
2     return x*x
3
4 print(pingfang(5))
5
6 y=pingfang(6)
7 print(y)

25
36

  • 习题  20:  函数和文件

 1 from sys import argv
 2
 3 #脚本名,参数一(导入的文件)
 4 script, input_file = argv
 5
 6 def print_all(f):       #定义一个新函数:将文件全打印出来
 7     print(f.read())
 8
 9 def rewind(f):          #定义新函数:将文件指针移动到最开头
10     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 let‘s print the whole file:\n")
18
19 print_all(current_file)
20
21 print("Now let‘s rewind, kinds of like a tape.")
22 rewind(current_file)
23
24 print("Let‘s print three lines:")
25
26 current_file = 1
27 print_a_line(current_file, current_file)
28
29 current_file = current_file + 1
30 print_a_line(current_file, current_file)
31
32 current_file = current_file + 1
33 print_a_line(current_file, current_file)

  • 习题  21:  函数可以返回东西

 1 def add(a, b):
 2     print("ADDING %d + %d" % (a, b))
 3     return a + b
 4
 5 def subtract(a, b):
 6     print("SUBTRACTING %d - %d" % (a, b))
 7     return a - b
 8
 9 def multipy(a, b):
10     print("MULTIPLAYING %d * %d" % (a, b))
11     return a * b
12
13 def divide(a, b):
14     print("DIVIDING %d / %d" % (a, b))
15     return a / b
16
17 print("Let‘s do some math with just functions!")
18
19 age = add(30, 5)
20 height = subtract(78,4)
21 weight = multipy(90,2)
22 iq = divide(100,2)
23
24 print("Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq))
25
26 #A puzzle for the extra credit, type it in anyway.
27 print("Here is a puzzle.")
28
29 what = add(age, subtract(height, multipy(weight, divide(iq,2))))
30
31 print("That becomes:\n",what,"\nCan you do it by hand?")

Let‘s do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLAYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50
Here is a puzzle.
DIVIDING 50 / 2
MULTIPLAYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes:
-4391.0
Can you do it by hand?

谜题解答

1 def jia(a,b,c,d,e):
2     print("谜题答案,一口气全算对\n %d + (%d - %d * %d / %d)" % (a,b,c,d,e))
3     return a+(b-c*d/e)
4
5 this=jia(35,74,180,50,2)
6 print(this)
7 print(this==what)

谜题答案,一口气全算对
35 + (74 - 180 * 50 / 2)
-4391.0
True

原文地址:https://www.cnblogs.com/mrfri/p/8452251.html

时间: 2024-08-30 10:59:35

python3 _笨方法学Python_日记_DAY4的相关文章

python3 _笨方法学Python_日记_DAY5

Day5 习题  24:  更多练习 1 print("Let's practice everything.") 2 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') 3 4 poem = """ 5 \tThe lovely world 6 with logic so firmly planted 7 cannot discern \n

python3 _笨方法学Python_日记_DAY6

Day6 习题  36:  设计和调试 If 语句的规则1. 每一个"if 语句"必须包含一个 else.2. 如果这个 else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做 die 的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误.3. "if 语句"的嵌套不要超过 2 层,最好尽量保持只有 1 层. 这意味着如果你在 if 里边又有了一个 if,那你就需要把第二个 if 移到另一个

python3 _笨方法学Python_日记_DAY7

习题 40: 字典, 可爱的字典 回顾一下列表: 列表可以通过数字,从0开始的数字来索引列表中的元素 而字典dict,可以通过任何东西找到元素,可以将一个东西和另一个相关联 还可以通过字符串向字典中添加 stuff = {'name': 'Zed', 'age': 18, 'height': 6**6} stuff['city'] = 'San Francisco' print(stuff['city']) stuff[1] = 'Wow' stuff[2] = 'Neato' print(st

笨方法学python3

阅读<笨方法学python3>,归纳的知识点 习题1:安装环境+练习  print函数使用  主要区别双引号和单引号的区别 习题2:注释符号# 习题3:运算符优先级,跟C/C++, Java类似 以下运算符优先级:从下往下以此递增,同行为相同优先级 Lambda #运算优先级最低 逻辑运算符: or 逻辑运算符: and 逻辑运算符:not 成员测试: in, not in 同一性测试: is, is not 比较: <,<=,>,>=,!=,== 按位或: | 按位异

Day 2 笨方法学Python

手打第25个练习,出错的地方有: def 定义后indent 4个空格,第一行空了以后,直接换行是跟上面对其的,但是运行时是错误的,我的解决方法是,重新手动空格4个: 还发现一个问题就是,中文解释,以前老是出错 # -*- coding : utf-8 -*- 网上看到的加上这个就可以了,但是我的还是出错.今天偶然在削微寒的博客http://www.cnblogs.com/xueweihan/的GIthub上找到了答案 #coding:utf-8 换成这个语句就可以了.以后,尽量每句都加上注释,

笨方法学python(5)加分题

这篇对应的是习题17,更多文件操作 # -*- coding: utf-8 -*- #对文件更多操作复制A文件的内容到B文件 #from sys import argv from os.path import exists prompt = "> " from_file = raw_input("please input the filename where you want to copy from: >") #in_file = open(from_

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

笨方法学python(4)加分题

自己在看笨方法学python这本书,把自己觉得有学到东西的记下来,并不是每个习题都有记录 这次对应的是:习题 6: 字符串(string)和文本 这次只要是想说明一下,在print语句中,只要带有格式化字符的,会当作格式化来处理 脚本1: 结果1: 打出的结果没有%r,那是因为当作格式化处理了 脚本2: 结果2: 会报错,因为print joke_evaluation %hilarious 是格式化的标识 脚本3: 结果3:

笨方法学Python3(21-44)

接着前天的总结 习题21:函数可以返回某些东西 定义函数的加减乘除,以及嵌套使用 习题22:回顾以前学的知识 习题23:字符串.字节串和字符编码 综合运用字符串.函数.文件读取等知识.详情如下: from sys import argv script, encoding, error = argv # 命令行参数处理 def main(language_file, encoding,errors): line = language_file.readline() # 就是用readline()处