ex29
# -*- coding: utf-8 -*-
people = 20
cats = 30
dogs = 15
if people < cats:
print "Too many cats! The world is doomed!"
if people > cats:
print "Not many cats! The world is saved!"
if people < dogs:
print "The world is drooled on!"
if people > dogs:
print"The world is dry!"
dogs += 5 # dogs = dogs + 5 ;x += y ,x = x + y
if people >= dogs:
print "People are greater than equal to dogs."
if people <= dogs:
print "People are less than equal to dogs."
if people == dogs:
print "People are dogs."
asd = 1
qwe = 2
if asd != qwe:
print "Oh!"
ex30
不论if 还是def 行尾 ‘:’ 的作用是告诉Python接下来将要创建一个新的代码区段,接下来的区段要有缩进
# -*- coding: utf-8 -*-
a = int(raw_input("please give me a number"))
b = int(raw_input("please give me a letter"))
print a + b
people = 5
cars = 40
buses = 15
if cars > people:
print "We should take the cars."
elif cars < people:
print "We should not take the cars."
else:
print "We can't decide."
if buses > cars:
print "That's too many buses."
elif buses < cars:
print "Mayble we could take the buses."
else:
print "We still can't decide."
if people > buses :
print "Allright, let's just take the buses."
else:
print "Fine, let's stay home then."
if people > cars and buses > cars:
print "Cars are the leastest."
else:
print "That's not right."
a = int(raw_input("please wirte a number."))# raw_input 可以接受任何类型,会把他们转成字符串类型输出。
if a <= 60:
print "don't pass."
elif a <= 80:
print "Oh! you're good." # else if在满足一个条件后,会直接输出,不再继续运行; 即只会运行它碰到的第一个是True 的区块
elif a <= 90:
print "Nice."
else:
print "Perfect" #int() 变成数字
else if在满足一个条件后,会直接输出,不再继续运行; 即只会运行它碰到的第一个是True 的区块
ex31
# -*- coding: utf-8 -*-
print "You enter the dark room with two doors. Do you go through door #1 or door #2?"
door = raw_input(">")
# 判断数字在某个区间中,1 < x < 10 or x in range(1, 10)
if door == "1":
print "There's a gaint bear eating a cheese cake. What do you do?"
print "1. Take the cakes."
print "2. Scream at the bear."
bear = raw_input(">")
if bear == "1":
print "The bear eats your face off. Good job!"
elif bear == "2":
print "The bear eats your legs off. Good job!"
else:
print "Doing %s is probably better. Bear runs away." % bear
elif door == "2":
print "You stare into the endless abyss at Cthulhu's retina."
print "1. Blueberries."
print "2. Yellow jacket clothespins."
print "3. Understanding revolvers yelling melodies."
insanity = raw_input(">")
if insanity == "1" or insanity == "2":
print "Your body survives powered by a mind of jello. Good job!"
else:
print "The insanity rots your eyes into a pool of muck. Good job!"
else:
print "You stumble around and fall on a knife and die. Good job!"
ex32
# coding=utf-8
the_count = [1,2,3,4,5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'apples', 2, 'orange', 3, 'pears', 4, 'apricots', 5]
#this first kind of for-loop goes through a list.
for number in the_count:
print "This is count %d"% number
#same as above
for fruit in fruits:
print "A fruit of type: %s." %fruit
# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print "I got %r." % i
# we can also build lists, first start with an empty one
elements = []
# then use the function to do 0 to 5 counts
for i in range(0,6):
print "Adding %d to the list." % i
#append is a function that lists undrestand
elements.append(i)
#now we can print them out too
for i in elements:
print "Element was: %d." %i
range 函数
range(1,5) #从1到5,不包含5
range(1,5,2)#从1到5,间隔2,不包含5
range(5) #从0到5,不包含5
#array = [1, 2, 5, 3, 6, 8, 4]
#>>> array[1:] 列出1以后的,从1开始
#>>>array[:-1] 列出-1 之前的,不包括-1
#>>>array[::2]
#[1, 5, 6, 4]
#>>> array[2::]
#[5, 3, 6, 8, 4]
#>>> array[::3]
#[1, 3, 4]
#>>> array[::-1]
#[4, 8, 6, 3, 5, 2 , 1]
#创建二维列表
# [[1,2],[4,5]]
array = [1,2,3,4]
'''这里讲列表本身并不包含数据1,2,3,4 而是包含变量array[0],array[1],array[2],array[3]
所以在del时,要del array[2],下几篇细讲
ex33
oridinal 有序,以1开始;cardinal 随机选取,以0开始 基数 = 序数-1
from sys import exit
exit(1) 表示程序发生了错误
exit(0) 表示程序是正常退出
# coding=utf-8
# oridinal 有序,以1开始;cardinal 随机选取,以0开始 基数 = 序数-1
# from sys import exit
# exit(1) 表示程序发生了错误
# exit(0) 表示程序是正常退出
i = 0
numbers = []
while i < 6:
print "At the top i is %d."% i
numbers.append(i)
i += 1
print "Numbers now:", numbers
print "At the bottom i is %d" %i
print "The numbers:"
for num in numbers:
print num
x = int(raw_input('>'))
def print_numbers(x):
i = 0
numbers = []
while i < x:
numbers.append(i)
i += 1
print numbers
print_numbers(x)
ex35
from sys import exit
# from sys import exit 加载退出项
def gold_room():
print "This room is full of gold. How much do you take?"
next = int(raw_input(">"))
if 0 in next or 1 in next:
how_much = int(next)
else:
dead("Man, you're not greedy, you win!")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def bear_rooom():
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 the bear?"
bear_move = False
while True:
next = raw_input(">")
if next == "take honey":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_move:
print "The bear has moved from the door. You can go through it now."
bear_move = True
elif next == "taunt bear" and bear_move:
dead("The bear gets pissed off and chews your leg off.")
elif next == "open door" and bear_move:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the grear evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or your head?"
next = raw_input(">")
if "flee" in next :
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
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 = raw_input(">")
if next == "left" :
bear_rooom()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()