Python control structure (2)※

1. Topic details

first question

Write code at the specified location to implement a function, which is to calculate and return the accumulated sum from 1 to n according to the input integer n( ).n≥0

If ninvalid, output None.

second question

Write code at the specified location to implement a function that calculates and returns the factorial based on the input integer n( nnot less than ) .0n

If ninvalid, output None.

third question

Write code at the specified location to implement a function, the function is to judge whether the given integer is a prime number.

fourth question

Write code at the specified location to implement a function with the same function as the following mathematical function.

 

fifth question

Write code at the specified location to implement a function that calculates personal income tax according to the following table.

Annual income tax rate
0~47,449 22%
47450~114,649 25%
114,650~174,699 28%
174,700~311,949 33%
311,950~ 35%

 

Two, the code

from random import *
from math import *

r = []
for i in [10, 200,300,400,500,600,700,800,900,1000]:
    seed(i)
    r.append(randint(1, i))

def print_(x):
    if type(x) == float:
        print("%.4f" % x)
    else:
        print(x)


#第一题

def sumInt(n):
    #请在下面编写代码
    # ********** Begin ********** #
    summary = 0
    if n < 0:
        summary  = None
    else:
        for i in range(n+1):
            summary += i
    # ********** End ********** #
    #请不要修改下面的代码
    return summary


#第二题

def factorial(n):
    #请在下面编写代码
    # ********** Begin ********** #
    fac = 1
    if n < 0:
        fac = None

    else:
        for i in range(1,n + 1): # 因为0的阶乘是1,所以rang(n),range(n+1)都不对
            fac *= i
    # ********** End ********** #
    #请不要修改下面的代码
    return fac

#第三题

def isPrime(n):
    # 请在下面编写代码
    # ********** Begin ********** #
    isprime = True
    if n == 1:
        isprime = False
    else:

        for i in range(2, int(pow(n, 0.5)) + 1):
            if n % i == 0:
                isprime = False

    # ********** End ********** #
    # 请不要修改下面的代码
    return (isprime)

#第四题

def f(x):
    # 请在下面编写代码
    # ********** Begin ********** #
    fx = 0
    if -10 <= x < -8:
        fx = x - 2
    elif -8 <= x <-6:
        fx = x + 3
    elif -6 <= x <= -2:
        fx = x * x
    elif -2 < x < 2:
        fx = abs(x)
    elif 2 <= x <= 4:
        fx = x ** 3
    elif 4 < x <= 6:
        fx = 3 * x - 4
    elif 6 < x <= 8:
        fx = 4 * x + 1


    # ********** End ********** #

    # 请不要修改下面的代码
    return fx


#第五题

def tax(salary):
    #请在下面编写代码
    # ********** Begin ********** #
    salaTax = 0
    if 0 <= salary <=47449:
        salaTax = salary * 0.22
    elif 47450 <= salary <=114699:
        salaTax = salary * 0.25
    elif 114650 <= salary <=174699:
        salaTax = salary * 0.28
    elif 174700 <= salary <=311949:
        salaTax = salary * 0.33
    elif salary >= 311950:
        salaTax = salary * 0.35

    # ********** End ********** #
    # 请不要修改下面的代码
    return salaTax

if __name__ == '__main__':
    for num in [-10, 0, 10, 100, 1000, 10000]:
        summary = sumInt(num)
        print(summary)
    print('\n***********************\n')

    for num in [-5, 0, 10, 15, 20, 25, 30]:
        fac = factorial(num)
        print(fac)
    print('\n***********************\n')

    for num in r:
        isprime = isPrime(num)
        print(isprime)
    print('\n***********************\n')

    for x in [-9, -8, -7, -6, -5, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]:
        fx = f(x)
        print(fx)

    print('\n***********************\n')
    for salary in [-1000, 0, 40000, 47450, 98000, 114650, 14980, 17470, 25670, 311950, 360000]:
        st = tax(salary)
        print_(st)


Guess you like

Origin blog.csdn.net/qq_43659681/article/details/124903494