习题34:访问列表的元素
1. 上网搜索一下关于序数(ordinal number)和基数(cardinal number)的知识并阅读一下。
2. 以你对于这些不同的数字类型的了解,解释一下为什么 “January 1, 2010” 里是 2010 而不是 2009?(提示:你不能随机挑选年份。)
3. 再写一些列表,用一样的方式作出索引,确认自己可以在两种数字之间互相翻译。
4. 使用 python 检查自己的答案。
感觉这道习题的主旨就是让我们明白,列表是从0开始计数的,而我们现实生活中却是从1开始计数的
习题35:分支和函数
from sys import exit
def goldRoom():
# 房间内充满金子,你愿意出多少钱?
print("This room is full of gold.How much do you take?")
# 请输入金额
next = input("please enter number> ")
# 这里只是判断输入的数字中是否包含0和1
if "0" in next or "1" in next:
howMuch = int(next)
else:
# 同志,请输入数字
dead("Man,learn to type a number.")
if howMuch < 50:
# 很好,你不是很贪婪,你赢了
print("Nice,you're not greedy,you win!")
exit(0)
else:
# 你太过贪婪了
dead("You greedy bastard!")
def bearRoom():
# 房间内有只熊
print("There is a bear here.")
# 熊正在吃蜂蜜
print("The bear has a bunch of honey.")
# 熊正在另一扇门前
print("The fat bear is in front of another door.")
# 我们怎样躲开这只熊
print("How are you going to move the bear?")
bearMoved = False
while True:
next = input("please enter take honey or taunt bear or taunt bear or open door> ")
if next == "take honey":
# 熊看着你,然后拍打你的脸
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bearMoved:
# 熊已经从门口走了,你现在可以过去了。
print("The bear has moved from the door.You can go through it now.")
bearMoved = True
elif next == "taunt bear" and bearMoved:
# 熊会生气,咬你的木头
dead("The bear gets pissed off and chews your log off.")
elif next == "open door" and bearMoved:
goldRoom()
else:
# 我不知道是什么意思。
print("I got no idea what means.")
def cthulhuRoom():
print("Here you see the great evil Cthulhu.")
print("He,it,whatever stares at you and you go insane.")
print("Do you flee for your life or eat your head?")
next = input("please enter flee or head> ")
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhuRoom()
# 输出原因并中断运行
def dead(why):
print(why, "Good job!")
exit(0)
def start():
print("You are in a dark room.")
print("There is a door to your right and left.")
print("Which one do you take?")
next = input("please enter left or right:> ")
if next == "left":
bearRoom()
elif next == "right":
cthulhuRoom()
else:
dead("You stumble around the room until you starve.")
start()
这段代码还是很有必要好好理解一下的,我也花了大概半小时
扫描二维码关注公众号,回复:
5099650 查看本文章
1. 把这个游戏的地图画出来,把自己的路线也画出来。
2. 改正你所有的错误,包括拼写错误。
3. 为你不懂的函数写注解。记得文档注解该怎么写吗?
4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?
5. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗?int() 这个函数可以给你一些头绪。
next.isdigit()
自带的方法
isdigit()——判断是否为数字
isalpha()——判断是否为纯字母
isalnum()——判断是否既有数字又有字母
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)