美文网首页
Python - 条件语句和循环(三)

Python - 条件语句和循环(三)

作者: 不给时间回忆 | 来源:发表于2018-03-08 21:03 被阅读0次

if/while/for三种语句的嵌套练习

  • 计算机出数,人猜数
  • 人出数,计算机猜数
  • 计算个人所得税
  • 按从小到大的顺序输出
  • 打印2到100内的素数
  • 打印图形
  • 水仙花数
  • 完美数
# 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
# 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
"""
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))))
# 输入三个数,按从小到大的顺序输出

a = int(input('a = '))
b = int(input('b = '))
c = int(input('c = '))
if a > b:
    a, b = b, a
if a > c:
    a, c = c, a
if b > c:
    c, b = b, c
# print('a = %d, b = %d, c = %d' % (a, b, c))
print(a, b, c)

# 等价于
(a, b) = a > b and (b, a) or (a, b)
(a, c) = a > c and (c, a) or (a, c)
(b, c) = b > c and (c, b) or (b, c)
print(a, b, c)
# 2到100内的素数打印出来
from math import sqrt

def search_prime(num):
    is_correct = True
    for factor in range(2, int(sqrt(num))):
        if num % factor == 0:
            is_correct = False
            break
    if not is_correct:
        pass
        #print('%d 不是一个素数' % num)
    else:
        print(num, end = '\t')
        #print('%d 是一个素数' % num)

for x in range(2, 101):
    search_prime(x)
#打印图形
"""
*
**
***
****

   *
  **
 ***
****

   *
  ***
 *****
*******
"""

row = int(input('请输入行数:'))

for z in range(1, row + 1):
    for rank in range(65, 65 + z):
        print(chr(64 + z), end = '')
    print()

for z in range(1, row + 1):
    #print(' ' * (row - z) + '*' * (z), end = '')
    for rank in range(1, row + 1):
        if rank > row - z:
            print("*", end = '')
        else:
            print(' ', end = '')
    print()

for z in range(1, row + 1):
    for rank in range(1, row * 2):
        if rank > row - z and rank <= row + z - 1:
            print('*',end = '')
        else:
            print(' ', end = '')
    print()
#水仙花数 从100到999打印出来

for num in range(100, 999):
    data = list(map(int, str(num)))# 将数字转换成列表
    a = data[0]
    b = data[1]
    c = data[2]
    if num == a ** 3 + b ** 3 + c ** 3:
        print('%d是一个水仙花数' % num)
# 完美数 10000以内
    
for num in range(1, 10000):
    f_sum = 0
    for factor in range(1, num):
        if num % factor == 0:
            f_sum += factor
    if f_sum == num:
        print('%d是一个完美数' % num)

相关文章

网友评论

      本文标题:Python - 条件语句和循环(三)

      本文链接:https://www.haomeiwen.com/subject/sibtfftx.html