1、判断字符串中有几个字符a
name = 'itheima is a brand of itcase'
count=0
for i in name:
if i == 'a':
#count=count+1
count+=1
print(f'itheima is a brand of itcase中共含有:{count}个字母a')
2、打印9*9乘法表
for i in range(1,10):
for j in range(1,i+1):
print(f"{j}*{i} = {j*i}\t", end=' ') # \t是对其格式
# end=' '为了输出不会换行
print()
3、案例:发工资案例
某公司,账户余额1W元,给20名员工发工资。
员工编号1-20,依次领取1000元
如果员工绩效分(1-10)(随机生成),如果低于5,不发工资,换下一位
如果工资发完了,结束发工资。
import random
num = random.randint(1, 10) # 包括10
staff = 20
money=10000
for i in range(1,staff+1):
num = random.randint(1, 10)
if num < 5:
print(f"员工{i},绩效分{num},低于5分,不发工资,下一位。")
continue
else:
if money < 1000:
print("工资发完了,下个月领取吧")
break
money = money - 1000
print(f"员工{i}发放工资1000元,账户余额还剩余{money}")