趣学python编程第六章答案_《趣学Python——教孩子学编程》学习笔记第4-6章

第4章 用海龟画图

4.1 使用Python的turtle(海龟)模块

turtle模块提供了编写向量图的方法,基本上就是画简单的直线、点和曲线。

4.1.1 创建画布

>>> import turtle

>>> t = turtle.Pen()

4.1.2 移动海龟

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(50)

>>> t.left(90)

>>> t.forward(50)

>>> t.left(90)

>>> t.forward(50)

>>> t.left(90)

>>> t.forward(50)

reset是重置命令,清除画布并把海龟放回开始的位置;clear 是清除命令,它只清楚屏幕,海龟仍留在原位。

up是把画笔从纸上抬起来,即停止作画,down是开始作画,以下代码是画两条平行线

>>> t.reset()

>>> t.backward(100)

>>> t.up()

>>> t.right(90)

>>> t.forward(20)

>>> t.left(90)

>>> t.down()

>>> t.forward(100)

第5章 用if和else来提问

5.1 if语句

>>> age = 13

>>> if age > 20:

print('You are too old!')

>>> age = 25

>>> if age > 20:

print('You are too old!')

You are too old!

5.2 条件语句帮我们作比较

>>> age = 10

>>> if age > 10:

print('You are too old for my jokes!')

>>> age = 10

>>> if age >= 10:

print('You are too old for my jokes!')

You are too old for my jokes!

>>> age = 10

>>> if age == 10:

print('You are too old for my jokes!')

You are too old for my jokes!

5.3 if-then-else语句

>>> print('Want to hear a dirty joke?')

Want to hear a dirty joke?

>>> age = 12

>>> if age == 12:

print('A pig fell in the mud')

else:

print('Shh. It' a secret.')

A pig fell in the mud

5.4 if和elif语句

>>> age = 12

>>> if age == 10:

print('What do you call an unhappy cranberry?')

print('A blueberry!')

elif age == 11:

print('What did the green grage say to the blue grape?')

print('Breathe! Breathe!')

elif age == 12:

print('What didi 0 say to 8?')

print('Hi guys!')

elif age == 13:

print('Why wasn't 10 afraid of 7')

print('Because rather eating 9,7,8 pi.')

else:

print('Huh?')

What didi 0 say to 8?

Hi guys!

5.5 组合条件

>>> age = 12

>>> if age == 10 or age == 11 or age == 12 or age == 13:

print('What is 13 + 49 + 84 + 155 + 97 ? A headache!')

else:

print('Huh?')

What is 13 + 49 + 84 + 155 + 97 ? A headache!

>>> age = 12

>>> if age >= 10 and age <= 13:

print('What is 13 + 49 + 84 + 155 + 97 ? A headache!')

else:

print('Huh?')

What is 13 + 49 + 84 + 155 + 97 ? A headache!

5.6 没有值得变量——None

None的含义是没有值。注意,None不是一个值为0的数字。

>>> myval = None

>>> print(myval)

None

>>> myval = None

>>> if myval == None:

print('The variable myval doesn't have a value')

The variable myval doesn't have a value

5.7 字符串与数字之间的不同

10与’10′不同,前者是数字,后者是字符串。但二者可以转换

>>> age = '10'

>>> if age > 9:

print('You are too old')

Traceback (most recent call last):

File "", line 1, in

if age > 9:

TypeError: '>' not supported between instances of 'str' and 'int'

>>> converted_age = int(age)

>>> if converted_age > 9:

print('You are too old')

You are too old

还可以做如下转换:

>>> age = 10

>>> converted_age = str(age)

>>> age = '10.5'

>>> converted_age = float(age)

第6章 循环

6.1 for循环

打印五次hello

>>> print('hello')

hello

>>> print('hello')

hello

>>> print('hello')

hello

>>> print('hello')

hello

>>> print('hello')

hello

打印五次hello,使用for循环

>>> for x in range(0,5):

print('hello')

hello

hello

hello

hello

hello

关于range的小插曲,range(0,5)表示从0开始到4结束。range和list结合,可以得到一个数字列表:

>>> print(list(range(10,20)))

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

我们也可以在print语句中用x来计算hello的个数

>>> for x in range(0,5):

print('hello %s' % x)

hello 0

hello 1

hello 2

hello 3

hello 4

用for循环遍历一个列表

>>> wizard_list = ['spider leg','toe of frog','eye of newt','bat win','slug butter','snake dandruff']

>>> for i in wizard_list:

print(i)

spider leg

toe of frog

eye of newt

bat win

slug butter

snake dandruff

还记得在第2章里我们做的那个算式吗?就是如果你用爷爷的疯狂发明复制金币的话,在一年后你将拥有多少金币的那个算式。20 + 10 * 365 – 3 * 52 它表示发现的20枚金币,再加上10个魔法金币乘以一年的365天,减去每周被乌鸦偷走的3枚金币。如果逆向看到这堆金币每周是怎么增长的呢?

magic_coins = 70,表示每天10个魔法币乘以一周的7天

>>> found_coins = 20

>>> magic_coins = 70

>>> stolen_coins = 3

>>> coins = found_coins

>>> for week in range(1,53):

coins = coins + magic_coins - stolen_coins

print('Week %s = %s' % (week,coins))

Week 1 = 87

Week 2 = 154

Week 3 = 221

Week 4 = 288

Week 5 = 355

Week 6 = 422

Week 7 = 489

Week 8 = 556

Week 9 = 623

Week 10 = 690

Week 11 = 757

Week 12 = 824

Week 13 = 891

Week 14 = 958

Week 15 = 1025

Week 16 = 1092

Week 17 = 1159

Week 18 = 1226

Week 19 = 1293

Week 20 = 1360

Week 21 = 1427

Week 22 = 1494

Week 23 = 1561

Week 24 = 1628

Week 25 = 1695

Week 26 = 1762

Week 27 = 1829

Week 28 = 1896

Week 29 = 1963

Week 30 = 2030

Week 31 = 2097

Week 32 = 2164

Week 33 = 2231

Week 34 = 2298

Week 35 = 2365

Week 36 = 2432

Week 37 = 2499

Week 38 = 2566

Week 39 = 2633

Week 40 = 2700

Week 41 = 2767

Week 42 = 2834

Week 43 = 2901

Week 44 = 2968

Week 45 = 3035

Week 46 = 3102

Week 47 = 3169

Week 48 = 3236

Week 49 = 3303

Week 50 = 3370

Week 51 = 3437

Week 52 = 3504

6.2 while循环

>>> x = 45

>>> y = 80

>>> while x < 50 and y < 100:

x = x+1

y = y+1

print(x,y)

46 81

47 82

48 83

49 84

50 85

>>> x = 1

>>> while x < 10:

if x == 5:

break

print(x)

x = x+1

1

2

3

4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值