Python的条件语句和循环
2018-02-28 19:19
183 查看
# 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))))
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))))
相关文章推荐
- Python条件语句与循环
- Python中条件选择和循环语句使用方法介绍
- Python 2.7 学习笔记 条件与循环语句
- python学习记录 — (3)条件语句与循环语句
- Python While循环语句 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为: while 判断条件:
- Python基础教程 第5章: 条件、循环和其他语句 学习笔记
- Python3.5基础——变量、数据结构、条件和循环语句、break与continue语句
- Python基础教程之第5章 条件, 循环和其它语句
- Python中的条件选择和循环语句
- Python中的条件选择和循环语句
- python 条件语句、循环语句
- python笔记(8)----条件、循环及其他语句
- python条件语句和循环语句简介
- python--条件, 循环和其他语句
- python 条件,循环和其他语句
- Python基础入门2--条件语句和循环语句
- python基础教程_学习笔记7:条件、循环、其它语句
- python条件循环和其他语句
- Python学习笔记-第五章 条件、循环和其他语句(上)
- python基础知识- 条件判断语句和循环语句