《笨办法学Python3》练习三十:else-if

2019-03-05  本文已影响0人  雨开Ame

练习代码

people = 30
cars = 40
trucks = 15

if cars > people:
    print("We should take the cars.")
elif cars < people:
    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 home then.")

Study Drills

  1. Try to guess what elif and else are doing.

  2. Change the numbers of cars, people, and trucks, and then trace through each
    if-statement to see what will be printed.

  3. Try some more complex Boolean expressions like cars > people or trucks < cars.

  4. Above each line write a comment describing what the line does.

上一篇 下一篇

猜你喜欢

热点阅读