python学习笔记(二)

# 一、流程控制结构
# 流程控制就是计算机执行代码的顺序,控制就是对计算机执行代码的顺序进行有效的管理,实现开发过程中的业务逻辑
# 顺序流程--代码一种自上而下的执行结构,也是python默认的流程
# 选择流程--根据在某一步的判断,有选择的去执行相应的逻辑的一种结构

# 分支流程--单分支--双分支--多分支 [if elif else:]
# 单分支
'''
if 条件表达式:
    一条条的python代码
    一条条的python代码
    ......
'''
# 双分支
import random

'''
if 条件表达式:
    一条条的python代码
    一条条的python代码
    ......
    else:
    一条条的python代码
    一条条的python代码
    ......
'''
# 多分支
'''
if 条件表达式:
    一条条的python代码
    一条条的python代码
    ......
    elif:
    一条条的python代码
    一条条的python代码
    ......
    elif:
    一条条的python代码
    一条条的python代码
    ......
    else:
    一条条的python代码
    一条条的python代码
    ......
'''

# 条件表达式:比较运算符,逻辑运算符,复合运算符

# 循环流程--在满足一定的条件下,一直重复的去执行某段代码的逻辑
'''
while 条件表达式:
    一条条的python代码
    一条条的python代码
    ......
    
for...in 可迭代集合对象:
   一条条的python代码
    一条条的python代码
    ......
    
    '''

# 单分支
'''
# score=int(input("输入你的成绩:"))
# if score<=60:
#     print("成绩不合格,继续加油")
#     pass #空语句
# print("-----结束-----")

# 双分支
# score=int(input("输入你的成绩:"))
# if score<=60:
#     print("成绩不合格,继续加油")
# else:
#     print("成绩合格,可参加下级考试")
#     pass
# print("-----结束-----")


# 多分支
# score=int(input("输入你的成绩:"))
# if score<0 or score>100:
#     score = int(input("重新输入你的成绩:"))
# elif score<60:
#     print("成绩不合格,继续加油")
# elif 60<=score<80:
#     print("成绩合格")
# elif 80<score<=95:
#     print("成绩良好")
# else:
#     print("成绩优秀")
'''

# 猜拳头小游戏 拳头--0,剪刀--1,布--2
'''
import random
person=int(input("请出拳:(拳头--0,剪刀--1,布--2)\n"))
computer=random.randint(0,2)
if person==0 and computer==1:
    print("你赢了")
elif person==1 and computer==2:
    print("你赢了")
elif person==2 and computer==0:
    print("你赢了")
elif person == 1 and computer == 0:
        print("你输了")
elif person == 2 and computer == 1:
        print("你输了")
elif person == 0 and computer == 2:
        print("你输了")
# elif person==computer:
#         print("平手")
#         pass
else:
    print("平手")
    pass
'''

# if else 的嵌套使用
'''
Credit=int(input("请输入你的学分:-----"))
#Grade=int(input("请输入你的成绩"))
if Credit>=10:
    Grade = int(input("请输入你的成绩:-----"))
    if Grade>=80:
        print("可以升级")
    elif 60<=Grade<80:
        print("继续努力,差一点就达到了呢")
    else:
        print("很遗憾噢,失败了")
else:
    print("太差了,重修修炼")
    pass
# 注意 else要和if在同一直线线上才表示一个if else 否则会报错,这就是python里面有着严格的缩进要求
'''

# 二、循环的分类 while for
# while 语法结构
# while 条件表达式:
#     代码指令
# 语法特点 1.有初始值 2.条件表达式 3. 变量[循环内计数变量]的自增自减,否则会造成死循环
# 使用条件:循环的次数不确定,是依靠魂环条件来结束 目的:为了相似或者相同的代码操作变得更见简洁,使得代码可以重复利用
# Example1--输出1-100之间的数据
'''
index=1#定义一个变量,一定得赋值
Sum=0
while index<=100:
    print(index)
    Sum = Sum + index
    # Sum+=index #也可以写成这种格式,含义一样的
    if index==100:
        print("1-100的数据和为:%s" %Sum)#使用%占位符表表示 %s-字符型 %变量名
        print("1-100的数据和为:{}" .format(Sum))#使用format函数表示 {} .format(变量名)
        pass
    index += 1#index=index+1
    pass
'''

# Example2--可多次进行的猜拳的小游戏-加上while循环
'''
count=1
a=0
print("九局定输赢")
while count<=9:
    import random
    person = int(input("请出拳:(拳头--0,剪刀--1,布--2)\n"))
    computer = random.randint(0, 2)
    if person == 0 and computer == 1:
        print("你赢了")
        a+=1
        print(a)
    elif person == 1 and computer == 2:
        print("你赢了")
        a+=1
        print(a)
    elif person == 2 and computer == 0:
        print("你赢了")
        a+=1
        print(a)
    elif person == 1 and computer == 0:
        print("你输了")
        a-=1
        print(a)
    elif person == 2 and computer == 1:
        print("你输了")
        a-=1
        print(a)
    elif person == 0 and computer == 2:
        print("你输了")
        a-=1
        print(a)
    else:
        print("平手")
        a=a+0
        print(a)
        pass
    count+=1
    pass
print("这局你赢了%s局,总的结果是:"%a)
if a>=5:
    print("这局你赢了")
else:
    print("这局你输了")
    pass
'''

# Example3--九九乘法表
'''
print("-----九九乘法表--正三角-----")
row=1 #表示行数
while row<=9:
    col=1
    while col<=row:
        # print("%d*%d=%d"%(row,col,row*col))
        print("%d*%d=%d" % (row, col, row * col),end=" ")#end的作用在这里是让输出在同一行上面
        # print("{}*{}={}" .format(row,col,row*col)) #使用format函数输出
        col+=1
        pass
    print()#print()函数本身就会换行,所以使用它就可以表示换行
    row+=1
    pass

print("-----九九乘法表--倒三角-----")
row=9
while row>=1:
    col=1
    while col<=row:
        # print("%d*%d=%d"%(row,col,row*col))
        print("%d*%d=%d" % (row, col, row * col),end=" ") #end的作用在这里是让输出在同一行上面
        # print("{}*{}={}" .format(row,col,row*col)) #使用format函数输出
        col+=1
        pass
    print() #print()函数本身就会换行,所以使用它就可以表示换行
    row-=1
    pass

# Example 打印直角三角形
row=10 #表示行数
while row>=1:
    col=1
    while col<=row:
        print("*",end=" ")
        col+=1
        pass
    print()
    row-=1
    pass

# Example 打印等腰三角形
row=1 #表示行数
while row<=5:
    i=1
    while i<=5-row: #控制打印空格的数量
        print(" ",end=" ")
        i+=1
        pass
    j=1
    while j<=2*row-1: #控制打印*号
        print("A",end=" ")
        j+=1
        pass
    print()
    row+=1
    pass

# Example 打印等腰三角形--倒三角
row=5 #表示行数
while row>=1:
    i=1
    while i<=5-row: #控制打印空格的数量
        print(" ",end=" ")
        i+=1
        pass
    j=1
    while j<=2*row-1: #控制打印*号
        print("A",end=" ")
        j+=1
        pass
    print()
    row-=1
    pass
'''

# 三、For循环的使用
# 语法特点--遍历操作,依次的取集合容器中的每个值
# for 临时变量 in 容器:
#     执行代码块
'''
Tags='谢嘉诚爱王旭' #字符串类型本身就是一个字符型的集合
for i in Tags:
    print(i)
    print(type(i))
    pass
#range 此函数可以生成一个数据集合列表--range函数的使用
#range(起始值,结束值,步长--步长不能为0)
sum=0
for date in range(1,11): # 左包含右边不包含
    sum+=date
    print(date,end=" ")
    pass
print() #换行
print("sum=%d"%sum)

# 求偶数和
sum=0
for date in range(1,11): # 左包含右边不包含
    if date%2==0: #%--取余,意思就是取余为0--能被2整除的数也就是偶数
        sum+=date
        # print(date, end=" ")
        print(date)
    # else:
    #     print('奇数=%s'%date)
    pass
print() #换行
print("sum=%d"%sum)
pass
'''

# 四、braek,continue的使用--只能用于循环语句当中
# break--代表中断结束的意思--满足条件直接结束本层循环--退出循环,continue--跳过本次循环,继续下一次循环

# Example1--break的使用
'''
sum=0
for i in range(1,51):
    if sum>100:
        print('本次循环结束\nsum的数值为sum=%d'%sum)
        break #退出循环
        pass
    sum+=i
    pass

# Example2--continue的使用
print('continue语句的使用')
for i in range(1,51):
    if i%2==0:
        continue
        print('在continue之后不会被执行')
        pass
    pass
    print(i,end=" ")
    pass
# break使用案例
for i in 'i love wangxu':
    if i=='w':
        break
        print(i)
        pass
    else:
        print(i,end="")
        pass

# continue使用案例
for i in 'i love wangxu':
    if i=='w':
        continue
    print(i,end='')
# 语法缩进规则严格
'''
# while--循环中--break,continue的使用
'''
index=1
while index<=10:
    if index>=5:
        break
        pass
    print(index,end=' ')
    index+=1
    pass
'''
# 九九乘法表--For循环来实现
'''
for i in range(1,10): # 从1到9
    for j in range(1,i+1): # 从1到i+1
        print('%d*%d=%d'%(i,j,i*j),end=' ')
        pass
    print() # 控制换行
    pass

# 九九乘法表--For循环来实现--倒三角
for i in range(9,0,-1): # 从9到1
    for j in range(1,i+1): # 从1到i+1
        print('%d*%d=%d'%(i,j,i*j),end=' ')
        pass
    print() # 控制换行
    pass
'''

# for--else--语句的使用
'''
for i in range(1,11):
    print(i,end=' ')
    if i>=5:
        break
        pass
    pass
else:
    print('就是在上面的循环当中,只要出现了break语句,那么else将不再被执行')
    print('已经执行完了吗')

# 登录账号模拟--三次登录错误将提示您
Account='xjc'
Password='123456'
for i in range(3):
    zh=input('请输入账号-----')
    pd=input('请输入密码-----')
    if Account==zh and Password==pd:
        print('恭喜您登录成功')
        break
        pass
    else:
        print('请重新输入账号密码,你还有 %d 次机会'%(2-i))
    pass
else:
    print('您的账号已经被系统锁定')
'''

# while--else--语句的使用
'''
# 登录账号模拟--三次登录错误将提示您
Account='xjc'
Password='123456'
index=1
while index<=3:
    zh=input('请输入账号-----')
    pd=input('请输入密码-----')
    if Account==zh and Password==pd:
        print('恭喜您登录成功')
        break
        pass
    else:
        print('请重新输入账号密码,你还有 %d 次机会'%(3-index))
        index+=11
    pass
else:
    print('您的账号已经被系统锁定')
    pass
'''

# random 的用法
# a=random.random() # 返回随机生成的一个浮点数,范围在[0,1)之间
# a=random.uniform(x,y) # 返回随机生成的一个浮点数,范围在[x,y)之间
# a=random.randint(1,100) # 生成指定范围内的整数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值