第六章——FOR、IF以及WHILE
1.if语句
1.1if
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
if people>=dogs:
print("People are greater than or equal to dogs")
if people<=dogs:
print("People are greater than or equal to dogs")
if people==dogs:
print("People are dogs.")
if语句对下面的代码起一个“开关”作用,如果if语句表达式为真true,就执行下面的代码块。
if 下面的代码缩进 4 个空格是为了告诉python与当前if语句匹配的程序代码。
如果没有缩进,你很可能收到一个错误提示。Python 一般会让你在一个带 : 的代码行下面缩进一些内容。
1.2 else和if
people = 30
cars = 40
trucks = 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 trucks > cars:
print("That's too many trucks.")
elif trucks < cars:
print("Maybe we could take the trucks.")
else:
print("We still can't decide.")
if people > trucks:
print("Alright, let's just take the trucks.")
else:
print("Fine, let's stay homethen.")
1.当条件互斥时使用if-elif-else,
2.else、elif为子块,不能独立使用,一个if语句中可以包含多个elif语句,但结尾只能有一个else语句
1.3 if嵌套使用
print("""You enyer a darker room with two doors.
Do you go through door #1 or door #2?""")
door = input('>')
if door == '1':
print("There's a giant bear here eating a cheese cake")
print("What do you do?")
print("1. Take the cake")
print("2 . Scream at the bear")
bear = 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(f"Well ,doing {bear} is probably better")
print("Bear runs away")
elif door == '2':
print("You stare into the endless abyss at Cthulhu retina")
print("1.Blueberries")
print("2.Yellow jacket clothespins")
print("3. Understanding revolvers yelling melodies.")
insanity = input('>')
if insanity =='1' or insanity =='2':
print("Your body survives powered by a mind of jello.")
print("Good job")
else:
print("The insanity rots your eyes into a pool of muck")
print("Good job")
else:
print("You stumble around and fall on a knife an die.Good job")
if 语句里面又放了一个 if 语句。这在创建“嵌套”(nested)
2.FOR语句
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
for number in the_count:
print(f"This is count {number}")
for fruit in fruits:
print(f"A fruit of type: {fruit}")
for i in change:
print(f"I got {i}")
elements = []
for i in range(0, 6):
print(f"Adding {i} to the list.")
elements.append(i)
for i in elements:
print(f"Element was: {i}")
range(0, 6) 中的 i 只循环了6次而不是7次? range() 函数只处理从第一个到最后一个数,但不包括最后一个数,所以它在 5就结束了。这是这类循环的通用做法
element.append() 的作用是把东西追加到列表的末尾
3.while语句
while-loop,只要一个布尔表达式是 True,while-loop 就会一直执行它下面的代码块。
i = 0
numbers = []
while i < 6:
print(f"At the top i is {i}")
numbers.append(i)
i = i + 1
print("Numbers now: ", numbers)
print(f"At the bottom i is {i}")
print("The numbers: ")
for num in numbers:
print(num)
结果展示:
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
for-loop 和 while-loop 的区别是什么? for-loop 只能迭代(循环)一些东西的集合,而 while-loop 能够迭代(循环)任何类型的东西。
exit(0) 是干什么用的? 在很多操作系统中,一个程序可以用 exit(0) 来结束,其中传入的数字代表是否有错误。如果你用 exit(1) 代表有 1 个错误, exit(0) 则代表程序正常退出。
为什么 input() 有时会被写成 input(’> ')? input 的参数是一个字符串,所以要在获取用户输入的内容前面加一个提示符。这里 > 也可以换成想要提示用户的文字。
|