pyhton习题练习

基础计算练习

2020-04-05  本文已影响0人  lvyz0207
import math

# 1. 输入 a、b、c 三个参数,求解 ax2+bx+c=0 的两个根,假定 b2-4ac>0。
def cal():
    flag = True
    while flag == True:
        a = int(input("请输入参数a:").strip())
        b = int(input("请输入参数b:").strip())
        c = int(input("请输入参数c:").strip())
        if b ** 2 - 4 * a * c <= 0:
            print("请重新输入a,b,c")
        else:
            flag = False
    x1 = (-1 * b + (math.sqrt(b ** 2 - 4 * a * c))) / (2 * a)
    x2 = (-1 * b - (math.sqrt(b ** 2 - 4 * a * c))) / (2 * a)
    print(x1, x2)


# cal()


# 2.输入 a、b、c 三个参数,以它们作为三角形的三边,判断是否可以构成一个三角形,
# 如 能则进一步计算其面积。三角形的面积 s 可以用以下公式计算: s=sqrt(p*(p-a)*(p-b)*(p-c))
# 其中 p=(a+b+c)/2。
def test2():
    flag = True
    while flag == True:
        a = int(input("请输入参数a:").strip())
        b = int(input("请输入参数b:").strip())
        c = int(input("请输入参数c:").strip())
        if a + b > c and a + c > b and b + c > a:
            flag = False
        else:
            print("输入不合法,请重新输入!")
    p = (a + b + c) / 2
    print(p)
    s = math.sqrt(p * (p - a) * (p - b) * (p - c))
    print(s)

# test2()

# 3.输入一个字母,判断它是否为小写英文字母。
def test3():
    a = input("请输入一个字母:")
    if a >= "a" and a<= "z":
        print("输入的是小写的哟")
    else:
        print("输入的不是小写字母")

# test3()
# 4.从键盘输入 5 个字符,统计’0’字符出现的次数。
def test4():
    list1 = []
    for i in range(5):
        a = input("输入字符,会让你连续输入5的哟:")
        list1.append(a)
    print(list1)
    count = list1.count("0")
    print(count)

# test4()

# 5.输入两个整数,判断哪个大并输出结果。
def test5():
    a = int(input("请输入一个整数:"))
    b = int(input("请输入一个整数:"))
    if a > b:
        print(a)
    else:
        print(b)
# test5()

# 6.输入一个字母,如它是一个小写英文字母,则把它变为对应大写字母输出。
def test6():
    a = input("输入一个字母:")
    if a <= "z" and a>="a":
        print(a.upper())
    else:
        print("输入的并非是小写字母",a)
# test6()
# 7.输入一个年份,判断它是否为闰年。
def test7():
    a = int(input("请输入year:"))
    if a % 4 == 0 and a%100 != 0 or a%400 == 0:
        print("{}是闰年".format(str(a)))
    else:
        print("{}不是闰年".format(str(a)))
# test7()
# 8.从键盘输入 a、b 两个数,按大小顺序输出它们。
def test8():
    a = int(input("请输入一个整数:"))
    b = int(input("请输入一个整数:"))
    if a>b:
        print(a,b)
    else:
        print(b,a)
# test8()

# 9.输入 a、b、c 三个整数,找出最小的数。
def test9():
    a = int(input("请输入一个整数:"))
    b = int(input("请输入一个整数:"))
    c = int(input("请输入一个整数:"))
    print(min(a,b,c))
# test9()
# 10.某企业发放的奖金根据利润提成。利润低于或等于 10 万元时,奖金可提 12%;利润高 于 10 万元,
# 低于 20 万元时,高于 10 万元的部分,可提成 8.5%;20 万到 40 万之间时,高 于 20 万元的部分,可提成 6%;
#40 万到 60 万之间时高于 40 万元的部分,可提成 4%;60 万 到 100 万之间时,高于 60 万元的部分,可提成 2.5%,
# 高于 100 万元时,超过 100 万元的部 分按 1%提成,从键盘输入当月利润,求应发放奖金总数?
def test10():
    li = int(input("请输入当月利润总额(万元):"))

    if li<=10:
        jiang = li*0.12
    elif li<20 and li>10:
        jiang = (li-10)*0.085+ 10*0.12
    elif li>= 20 and li < 40:
        jiang = 10*0.085+ 10*0.12 + (li-20)*0.006
    elif li>= 40 and li < 60:
        jiang = 20*0.006 + 10*0.085+ 10*0.12 + (li-40)*0.004
    elif li>= 60 and li < 100:
        jiang = 20*0.006 + 10*0.085+ 10*0.12 + 20*0.004 + (li-60)*0.25
    else:
        jiang = 20 * 0.006 + 10 * 0.085 + 10 * 0.12 + 20 * 0.004 + 40* 0.25 + (li - 100) *0.001
    print(jiang)

# test10()

# 11.平面上有四个圆,圆心分别为(2,2),(-2,2),(-2,-2),(2,-2),圆半径为 1。今 输入任一点的坐标,判断该点是否在这四个圆中,如在则给出是在哪一个圆中。
def test11():
    x= float(input("请输入横坐标:"))
    y = float(input("请输入纵坐标:"))
    distance = math.sqrt((abs(x)-2)**2 + (abs(y)-2)**2)
    if distance > 1:
        print("点没有在四个圆内")
    else:
        if x > 0 and y >0:
            print("点在(2,2)圆内")
        elif x < 0 and y >0:
            print("点在(-2,2)圆内")
        elif x < 0 and y <0:
            print("点在(-2,-2)圆内")
        else:
            print("点在(2,-2)圆内")
test11()
貂蝉_逐梦之音.jpg
上一篇 下一篇

猜你喜欢

热点阅读