《笨方法学PYTHON》——fiftteenthlesson

习题29:如果(if)

people = 20
cats = 30
dogs = 15
if people < cats:
    print("Too many cats!Theworld 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 less than or equal to dogs.")
if people == dogs:
    print("People are dogs.")
Too many cats!Theworld is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

1. 你认为 if 对于它下一行的代码做了什么?

True则执行,False则不执行

2. 为什么 if 语句的下一行需要 4 个空格的缩进?

python就是通过这种方式来的,相当于java中{},只要是缩进的,都是这个条件下的代码

3. 如果不缩进,会发生什么事情?

通通执行

4. 把习题 27 中的其它布尔表达式放到``if语句``中会不会也可以运行呢?试一下。

5. 如果把变量 people, cats, 和 dogs 的初始值改掉,会发生什么事情

改成不同的值,执行的if就不一样呗,总之,True执行,False不执行

习题30:Else和If

people = 30
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("Maybe we could take the buses.")
else:
    print("We still can't decide.")
if people > buses:
    print("Alright,let's just take the buses.")
else:
    print("Fine,let's stay home then.")
We should take the cars.
Maybe we could take the buses.
Alright,let's just take the buses.

1. 猜想一下 elif 和 else 的功能。

elif需要加条件,else就是其他的

2. 将 cars, people, 和 buses 的数量改掉,然后追溯每一个 if 语句。看看最后会打印出什么 来。

3. 试着写一些复杂的布尔表达式,例如 cars > people and buses < cars。

4. 在每一行的上面写注解,说明这一行的功用。

作者在注意点里提到一个知识点,感觉还是挺重要的,if和elif,遇到第一个True就执行,其他的计算机是不看的。

习题31:作出决定

print("You enter a dark 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.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("Well,dong %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 = input("> ")
    if insanity == "1" or insanity == "2":
        print("Your boby 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!")
> 1
There's a giant bear here eating a cheese cake.What do you do?
1.Take the cake.
2.Scream at the bear.
> 1
The bear eats your face off.Good job!

这几篇讲的if-else,其实很简单,不都说嘛,中级以下的程序员都是些if-else,学会这个就可以当程序员了,哈哈哈

本人是做测试的,目前代码能力就just so so吧,但实话实说,那些程序员也just so so,会写罢了,当然高级另算哈。最可气的是,这个CSDN竟然没有给测试这个分类,你说可气不可气,测试这门水这么深,竟然没有这个分类

print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)

猜你喜欢

转载自blog.csdn.net/qq_41470573/article/details/84728874