Python_分支、循环、条件、枚举

一.if 判断

代码段

user = 'joy'
password = '123456'
# 输入用户名和密码
user_account = input('please input user:')
user_password = input('please input password:')
# 判断用户名和密码是否正确 if user == user_account and password == user_password : print('登录成功') else: print('用户名或密码错误')

  输出结果

please input user:abb
please input password:2387
用户名或密码错误

二.elif 多分支

代码段A     (使用 if + else 实现)

result = int(input("你的成绩是:"))

if result > 80 and result <= 100 :
    print("优秀成绩")
else : 
    if result <= 80 and result >= 60 :
        print("中等成绩")
    else:
        if result < 60 :
            print("成绩较差")
        else :
            print("成绩异常") 

代码段B   (使用 if + elif + else 实现)

result = int(input("你的成绩是:"))    # input默认为str类型,需要转换成int类型

if result > 80 and result <= 100 :
    print("优秀成绩")
elif result <= 80 and result >= 60 :
    print("中等成绩")
elif result < 60 :
    print("成绩较差")
else :
    print("成绩异常")

  输出结果

你的成绩是:65
中等成绩

三.while 循环

代码段  

# while 递归
a = 1
c = 1
while a <= 100 :
    b = 0
    while b <= 9 :
        b += 1
        print(a,end=" ")  # 打印一个数字,end留一个空格
        a += 1
    print("".strip("\n"))   # strip去除字符串首位转义字符,换行、空格
else :
    print("end")

  输出结果

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
end

四.for 循环

主要用来遍历、循环、序列或者集合、字典

代码段

goods = [['ipad','iphone','iwatch','imacbook','headphone'],(4800,6500,3200,150000,2400)]
for i in goods:    
    for x in i:
        print(x,end=" ")
else:
    print('fruit is gone')

  输出结果

ipad iphone iwatch imacbook headphone 4800 6500 3200 150000 2400 fruit is gone

1.break 跳出

代码段

goods = ['ipad','iphone','iwatch','imacbook','headphone']

for i in goods:
    if i == 'iwatch':
        break           # 跳出,终止程序
    print(i)

 输出结果

ipad
iphone

2.continue   跳过、继续

代码段

goods = ['ipad','iphone','iwatch','imacbook','headphone']

for i in goods:
    if i == 'iwatch':
        continue           # 跳过,继续
    print(i)

  输出结果

ipad
iphone
imacbook
headphone

3.嵌套循环

代码段

goods = [['ipad','iphone','iwatch','imacbook','headphone'],(4800,6500,3200,150000,2400)]
for i in goods:
#    print(i)    
   for x in i:
    #    print(x)
        if x == 'iwatch':
            break        # break只会影响同一级的for循环,其他for循环正常运行
        print(x,end=" ")
else:
    print('fruit is gone')

  输出结果

ipad
iphone
imacbook
headphone
ipad iphone 4800 6500 3200 150000 2400 fruit is gone

4.阵列

代码段

for x in range(2,101,2):
    print(x,end='|')
print("\n")

for y in range(100,1,-2):   # 倒序
    print(y,end='|')

  输出结果

2|4|6|8|10|12|14|16|18|20|22|24|26|28|30|32|34|36|38|40|42|44|46|48|50|52|54|56|58|60|62|64|66|68|70|72|74|76|78|80|82|84|86|88|90|92|94|96|98|100|

100|98|96|94|92|90|88|86|84|82|80|78|76|74|72|70|68|66|64|62|60|58|56|54|52|50|48|46|44|42|40|38|36|34|32|30|28|26|24|22|20|18|16|14|12|10|8|6|4|2|

5.列表阵列

代码段

number = [1,2,3,4,5,6,7,8,9,10]
for a in number[0:len(number):2]:   # 方法一:使用切片
    print(a,end=' ')
print("\n")
for b in range(1,len(number),2):    # 方法二:使用range方法
     print(b,end=' ')

  输出结果

1 3 5 7 9

1 3 5 7 9

  

  

number = [1,2,3,4,5,6,7,8,9,10]
for a in number[0:len(number):2]:   # 方法一:使用切片
    print(a,end=' ')
print("\n")
for b in range(1,len(number),2):    # 方法二:使用range方法
     print(b,end=' ')
posted @ 2020-02-23 10:55  Cheney!努力才会幸运  阅读(239)  评论(0编辑  收藏  举报