分支结构和循环结构

上节课的回顾

print(f'{a:+> 30.3f }')其中的30.3f 的30表示使用30个字符, .3f表示保留小数点后3位,可参照下面:
# 获得用户输入的一个整数a,计算a的平方根,保留小数点后3位,并打印输出。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬
# 输出结果采用宽度30个字符、右对齐输出、多余字符采用加号(+)填充。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬
# 如果结果超过30个字符,则以结果宽度为准。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬
# 示例:
# 输入:10 ,输出:+++++++++++++++++++++++++3.162
# a = int(input('请输入一个整数:'))
# a_squa = pow(a, 0.5)
# print(f'{a_squa:+>30.3f}')
# 获得输入的一个字符串s,以字符减号(-)分割s,将其中首尾两段用加号(+)组合后输出。
# 示例:
# 输入:Alice-Bob-Charis-David-Eric-Flurry,输出:Alice+Flurry
# s = input('请输入字符串:')  
# res = s.split('-')
# print(f'{res[0]}+{res[-1]}')
# 数字类型
## 整形
x = int(10)
pow(x, 10)
## 浮点型
y = float(10)  # 10.0
print(round(0.1 + 0.2, 1) == 0.3)

分支结构

1.单分支(if)
# if一般用于判断/选择的场景
# 90以上优秀
# score = 95
# if score > 90:
# print('优秀')
结果为:
优秀
2.双分支(if.....else:....)
# 90以上优秀,90一下良好
##写法1
# score = 95
# if score > 90:
#     print('优秀')
# else:
#     print('良好')
##写法2
# score = 75
# print('优秀') if score > 90 else print('良好')  # 单分支没有,多分支也没有
#     结果一         条件         结果二

结果为:
优秀
3.多分支(if.....elif.....elif.....else:....)
## if...elif...elif...else和 if...if...if...if的区别
## 90以上优秀,90-70良好,70以下及格
score = 95
if score > 90:
    print('优秀')
elif score > 70:
    print('良好')
else:
    print('及格')
结果为:
优秀
    
score = 95
if score > 90:
    print('优秀')
if score > 70 and score < 90:
    print('良好')
if score < 70:
    print('及格')
 结果为:
优秀
良好

逻辑运算符

## > >= < <= == !=
## and 两者都必须成立
## or 其中一个成立即可
## not 非

异常处理

1.try.....except+异常错误名+as+自定义异常错误名的代替名:.....
x=10
try:
 1/0 # ZeroDivisionError
 y=input("请输入数字:")
 y+=10 #TypeError
except TypeError as e:
    print(f'error:\033[1;35m {e} \033[0m!')
except ZeroDivisionError as e:
    print(f'error:\033[1;35m {e} \033[0m!')
print(x+10)

结果为:

1739655-20190717192744907-435245181.png

2.try.....except+Exception+as+自定义异常错误名的代替名:.....(主要)
x=10
try:
    y=input("请输入数字:")
    y+=10
except Exception as e:
    print(f'error:\033[1;35m {e} \033[0m!')
finally:      #无论报不报错,都会执行finally下面的代码
    print(1)

结果为:

1739655-20190717192926461-1684639527.png

raise 自定义报错(了解)
s=input("请输入数字:")
print(s.isalpha())

结果为:

1739655-20190717193053697-1465737417.png

1739655-20190717193102139-213810113.png

assert 断言(了解)
assert 1==2

结果为:

1739655-20190717193220263-1971205602.png

循环结构

1.for循环
for i in range(100):
    print(i)

for i in 'nick':
print(i,end="")
结果为:
nick
range
range(5) # [0,1,2,3,4]
range(1,3)  # [1,2]
range(1,10,2) # [1,3,5,7,9]
2.while循环
factory=0.01
base=0
while base<pow(1.01,365):
    factory+=0.0001
    base=1
    for i in range(365):
        if i%7==6:
            base*=(1-0.01)
        elif i%7==0:
            base *= (1 - 0.01)
        else:
            base*=(1+factory)
print(factory)
continue和break
count=0
while count<100:
    if count==49:
        count+=1
        continue    # 不执行下面代码,继续运行循环
    count+=1
    print(count)

结果为:

1739655-20190717193424432-266272919.png

count=0
while count<100:
    if count==49:
        count+=1
        break    # 终止循环
    count+=1
    print(count)

结果为:

1739655-20190717193534048-329260960.png

count = 0
while count < 100:
    if count == 49:
        count += 1
        break  # 终止循环
    count += 1
    print(count)
else:  #正常跳出循环的时候会执行,异常中断不执行
    print('打我')

结果为(当while后的条件为true时结果也是这个结果):1739655-20190717193534048-329260960.png

将上面的break改为continue就会出现如下结果

1739655-20190717193901704-48626564.png

random(产生随机数)

 # 需要导入random库 import random
import  random
print(random.randint(1,10))#随机产生1到10之间的随机数

import  random
print(random.random())#随机产生0到1之间的小数

import random
random.seed(10)#随机种子固定,因而产生的随机数固定
print(random.random())
#如果不定义种子,则种子按照当前的时间来
import random
print(random.choice([1,1,2,3,4,5]))#随机选择[1,1,2,3,4,5]中的数
import random
lt = [1,2,3,4]
random.shuffle(lt)#打乱lt
print(lt)
自己制作随机数
#产生随机数
import time
time_ = time.time()
print(str(time_).split('.')[-1][-1])

圆周率

#蒙特卡洛方法求Π
import random
count = 0
for i in range(100000):
    x, y = random.random(), random.random()
    dist = pow(x ** 2 + y ** 2, 0.5)
    if dist < 1:
        count += 1
print(count / 100000 * 4)

转载于:https://www.cnblogs.com/suixi/p/11203214.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值