Python的条件语句和循环

本博客包含多个使用Python编写的简单游戏,如猜数字、绘制图形、猜拳等,并附带了一个计算个人所得税的小程序。通过这些游戏,读者可以了解Python的基础语法及控制流程。
# 1到100 的随机数,计算机出数,人猜数字
from random import randint


is_correct = False
count = 0
index = randint(1, 100)
print(index)
while not is_correct: # while True 条件恒成立
    thy_answer = int(input('请输入数字:'))
    if thy_answer == index:
        print('恭喜你答对啦!')
        is_correct = True # break 跳出循环
    elif thy_answer > index:
        print('数字偏大!')
        is_correct = False
    else:
        print('数字偏小!')
        is_correct = False
    if count > 7:
        print('你的智商余额不足,请尽快充值!')

    count += 1


#1、用turtle模块绘制更复杂的图形
import turtle as tr


"""
# 画太阳花
tr.pencolor('red')
tr.begin_fill()
tr.fillcolor('yellow')
while True:
    tr.forward(300)
    tr.left(170)
    if abs(tr.pos()) < 1:# 画笔是否回到原点
        break
tr.end_fill()
tr.hidetr()
tr.mainloop()


"""
# 画几何图形
tr.pencolor('red')
count = 1
tr.left(20)
lenght = 20
while count < 24:
    if count % 6 == 0:
        lenght += 10
    tr.forward(lenght)
    for _ in range(5):
        tr.right(60)
        tr.forward(lenght)
    count += 1
tr.hideturtle()

tr.mainloop()

# 2、反转的猜数字(人出数,计算机猜)
from random import randint


is_correct = False
count = 0
index = int(input('请输入数字:'))
answer = 50
while not is_correct: # while True 条件恒成立
    
    if answer == index:
        print('恭喜你答对啦! %d' % answer)
        is_correct = True # break 跳出循环
    elif answer > index:
        print('数字偏大! %d' % answer)
        answer = randint(0,answer)
        is_correct = False
    else:
        print('数字偏小! %d' % answer)
        answer = randint(answer, 100)
        is_correct = False


    #count += 1
    #if count > 7:

     #   print('你的智商余额不足,请尽快充值!')


#3、人机猜拳(计算机产生随机数表示剪刀石头布)
# 1:剪刀 2:石头 3:布
from random import randint


p_score = 10
c_score = 10
score = 2
while True:
    if p_score <= 0:
        print('一号选手输了!')
        break
    if c_score <= 0:
        print('二号选手输了!')
        break
    p_num = int(input('一号选手出拳:'))
    c_num = randint(1, 3)
    print('二号选手出拳:%d' % c_num)
    
    if p_num == c_num:
        pass
    elif p_num == 1 and c_num == 2:
        p_score -= score
        c_score += score
    elif p_num == 1 and c_num == 3:
        p_score += score
        c_score -= score
    elif p_num == 2 and c_num == 1:
        p_score += score
        c_score -= score
    elif p_num == 2 and c_num ==3:
        p_score -= score
        c_score += score
    elif p_num == 3 and c_num == 1:
        p_score -= score
        c_score += score
    elif p_num == 3 and c_num == 2:
        p_score += score
        c_score -= score
    print('一号选手的分数:%d, 二号选手的分数:%d' % (p_score, c_score))

  

"""
4、计算个人所得税
总收入 - 五险一金 > 3500
(总收入 - 五险一金 - 3500) * 税率 - 速算扣除数
15800 - 3200 - 3500
"""
totalMoney = float(input('请输入税前工资:'))
# python2 的input输入的数据类型跟随数据转换的  raw_input不带类型转换
safeMoney = float(input('请输入五险一金:'))
baseMoney = 3500 # 工资起征点
# rate1:税率
# data:速算扣除数
currentdata = (totalMoney - safeMoney - baseMoney) # 全月应纳税所得额
if currentdata <= 0:
    rate = 0
    data = 0
elif currentdata <= 1500:
    rate = 0.03
    data = 0
elif currentdata <= 4500:
    rate = 0.1
    data = 105
elif currentdata <= 9000:
    rate = 0.2
    data = 555
elif currentdata <= 35000:
    rate = 0.25
    data = 1005
elif currentdata <= 55000:
    rate = 0.3
    data = 2755
elif currentdata <= 80000:
    rate = 0.35
    data = 5505
else:
    rate = 0.45
    data = 13505
print('个人所得税:¥%.2f' % abs((currentdata * rate - data)))
print('税后工资:¥%.2f' % (totalMoney - safeMoney - abs((currentdata * rate - data))))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值