习题34:访问列表的元素
序数(ordinal number):从1开始编号,表示事物的顺序
基数(cardinal number):从0开始,表示你可以任意抓取元素
1. 上网搜索一下关于序数(ordinal number)和基数(cardinal number)的知识并阅读一下。
2. 以你对于这些不同的数字类型的了解,解释一下为什么 “January 1, 2010” 里是2010 而不是 2009?(提示:你不能随机挑选年份。)
年份是序数,从1开始,没有0年
3. 再写一些列表,用一样的方式作出索引,确认自己可以在两种数字之间互相翻译。
4. 使用 python 检查自己的答案。
-
animals = [
'bear',
'python',
'peacock',
'kangaroo',
'whale',
'platypus']
-
# 熊、孔雀、袋鼠、鲸、鸭嘴兽
-
for i
in range(
0,
6):
-
print(
"The animal at %d is the %dst animal and is a %s" % (i, i+
1, animals[i]))
习题35:分支和函数
-
from sys
import exit
-
def gold_room():
-
print(
"The room is full of gold. How much do you take?")
-
choice = input(
">")
-
if
"0"
in choice
or
"1"
in choice:
-
how_much = int(choice)
-
else:
-
dead(
"Man, learn to type a number.")
<