0
点赞
收藏
分享

微信扫一扫

学习笔记 task6--for、if以及while

做个橙梦 2022-02-26 阅读 13
学习python

1.IF语句

people=20
cats=30
dogs=15

if people<cats:
    print("too many cats")
    if people<dogs:
        print("too many dogs")
    else:
        print("the world doesn't have so many dogs")
else:
    print("cats are not so much, the world is normal")

    
print('-'*20)
dogs +=15
people+=15

if people<cats:
    print("too many cats")
else:
    print("cats are not so much, the world is normal")
    
if people<dogs:
    print("too many dogs")
else:
    print("the world doesn't have so many dogs")
too many cats
the world doesn't have so many dogs
--------------------
cats are not so much, the world is normal
the world doesn't have so many dogs

if对下面的代码起条件判断的作用

people=30
cars=40
trucks=15

if cars>people:
    print("we should take cars")
elif cars==people:
    print("we can't decide")
else:
    print("we should not take cars")
we should take cars

if嵌套使用:

# 分数评级
score=int(input("请输入您的分数:"))
if score>=90:
    print("your grade is : A")
elif score>=80:
    print("your grade is : B")
elif score>=70:
    print("your grade is : C")
elif score>=60:
    print("your grade is : D")
else:
    print("your grade is : E")
请输入您的分数:88
your grade is : B

2.For语句

L=[]

def my_func(x):
    return 2*x

for i in range(5):
    L.append(my_func(i))  #对序列L依次增加0,2,4,6,8
    
L
[0, 2, 4, 6, 8]
[my_func(i) for i in range(5)] #列表推导式
[0, 2, 4, 6, 8]

3.while语句

i=0
numbers=[]

while i <6:
    print(f"at the top i is {i}") #打印每一步循环中i的值
    numbers.append(i)  #将i的值依次添加到列表numbers
    
    i +=1
    print(f"numbers now is : {numbers}")
    print(f"at the bottom i is {i}")
    print("="*20)
    
print(f"the numbers:{numbers}")

for num in numbers:
    print(num)
at the top i is 0
numbers now is : [0]
at the bottom i is 1
====================
at the top i is 1
numbers now is : [0, 1]
at the bottom i is 2
====================
at the top i is 2
numbers now is : [0, 1, 2]
at the bottom i is 3
====================
at the top i is 3
numbers now is : [0, 1, 2, 3]
at the bottom i is 4
====================
at the top i is 4
numbers now is : [0, 1, 2, 3, 4]
at the bottom i is 5
====================
at the top i is 5
numbers now is : [0, 1, 2, 3, 4, 5]
at the bottom i is 6
====================
the numbers:[0, 1, 2, 3, 4, 5]
0
1
2
3
4
5
def generate_list(n):  #定义一个输入数字n,输出0-n之间(不含n)的列表
    list=[]
    i=0  #注意要定义i的初始值
    while i<n:
        list.append(i)
        i +=1
    return list

generate_list(6)

[0, 1, 2, 3, 4, 5]
generate_list(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

改变增加的值

def generate_list(n):  #定义一个输入数字n,输出0-n之间(不含n)的列表
    list=[]
    i=0  #注意要定义i的初始值
    while i<n:
        list.append(i)
        i +=2      #改变增加的值
    return list

generate_list(20)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

用FOR循环写:

def generate_list2(n):
    list=[]
    for i in range(n):
        list.append(i)
    return list
  
generate_list2(6)
[0, 1, 2, 3, 4, 5]

用FOR循环不需要中间的增加值

for和while的区别是什么?

for循环只能迭代循环一些东西的集合,while能迭代循环任何类型的东西。但for更简洁通用。

分支和函数

from sys import exit

def dead(why):
    print(why,"good job")
    exit(0)

def gold_room():
    print("this room is full of gold. how much would like to take ?")
    
    choice=input("please input your choice: ")
    if "0" or "1" in choice:
        how_much=int(choice)
    else:
        dead("man, learn to type a number.")  # dead是什么?后面定义的一个函数
        
    if how_much<50:
        print("nice, you are not greedy,you win")
        exit(0)
    else:
        dead("you greedy bastard")
        
def bear_room():
    print("there is a bear here.\n the bear has a bunch of honey.\n the fat bear is in front of another door.\n how are you going to move the bear?")
    bear_moved=False
    
    while True:
        choice=input("please input your choice: ")
        
        if choice == "take money":
            dead("the bear looks at you the slaps your face...")
        elif choice=="taunt bear" or bear_moved:
            print("the bear has moved from the door.\n you can go through it now.")
            bear_moved=True
        elif choice =="open door" or bear_moved:
            gold_room()
        else:
            print("I got no idea what that means.")
            
def cthu_room():
    print("here you see that great evil clutlhu.")
    print("he,it,whether stares at your life or eat your head ?")
    
    choice=input("please input: ")
    
    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("well that's tasty!")
    else:
        cthu_room()
    
def start():
    print("you are in a dark room.\n there is a door to your left and right\n which one would you like to open?")
    
    choice=input("please input: ")
    
    if choice=="left":
        bear_room()
    elif choice=="right":
        cthu_room()
    else:
        dead("you stumble around the room untill you starve.")
        
start()
you are in a dark room.
 there is a door to your left and right
 which one would you like to open?
please input: left
there is a bear here.
 the bear has a bunch of honey.
 the fat bear is in front of another door.
 how are you going to move the bear?
please input your choice: open door
this room is full of gold. how much would like to take ?
please input your choice: 1
nice, you are not greedy,you win



An exception has occurred, use %tb to see the full traceback.


SystemExit: 0



C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3351: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

举报

相关推荐

0 条评论