News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Python123 期末题库

posted on 2023-05-21 18:18     read(1512)     comment(0)     like(30)     collect(4)


foreword

  This article is recorded in the question bank on Python123, the code is for reference only, and some questions have been collected in addition to school work. For students who are beneficial, you can bookmark it, write your feelings in the comment area, don't pay attention, social fear, thank you!

Question bank

1. Hello World I

Output Hello World, pay attention to capitalization.

print('Hello World')

This is too simple, don't just wait for failing, hahaha.

2. Say something from your heart A

Two inputs from the user are received from the console twice: the first content is "person's name", and the second content is "heart-to-heart".
Then the two input contents are composed into the following sentence pattern and output: ‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪
( person's name), I want to say to you, (heart speaking)

a=input("")
b=input("")
print(f'{a:},我想对你说,{b:}')

  Holds the input and f' ' controls the output format.

3. Calculate the matrix area A

The user inputs the length and width of the rectangle, calculates its area and outputs it.

a, b = eval(input()), eval(input())
print(a*b)

  Pay attention to the format of the input as in the fourth question.

4. Output the integer part and fractional part of a number

The user is required to input a decimal, and the integer part and the decimal part can be obtained separately and output.

zs,xs=input().split('.')
print(f'整数部分是{zs},小数部分是{xs}')

5. Triangle perimeter and area

The lengths of the three sides a, b, and c of the input triangle, calculate and output the perimeter and area of ​​the triangle in turn. The data of the test case guarantees that the data of the three sides of the triangle can form a triangle.

import math
a = eval(input())
b = eval(input())
c = eval(input())
# 保证三角形存在不用判断了
s = (a+b+c)/2
squar = math.sqrt(s*(s-a)*(s-b)*(s-c))
perimeter = a+b+c
print('周长={:.2f}'.format(perimeter))
print('面积={:.2f}'.format(squar))

  It's all formulas, very simple. The math library can be used here, and the root can be ** 0.5 . The three inputs can be written in one line.

6. Expression evaluation

Use the following formula to calculate and output the value of x. ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬ ‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬ (Hint: The square root can be realized by the exponential operation a**(1/2), and the math library math is also introduced, and then the math.sqrt() function in the math library accomplish)

import math
a = eval(input())
b = eval(input())
c = eval(input())
d = (-b+math.sqrt(b*b-4*a*c))/(2*a)
print("{:.2f}".format(d))

7. Calculate deposit interest

Enter the initial deposit amount, deposit period, and annual interest rate in the three lines in turn, calculate the interest at the end of each year and convert it to the principal, calculate and output the interest when the deposit is due (excluding the principal, before tax), and reserve 2 digits for the result decimal.

import math
P = eval(input())
N = eval(input())
i = eval(input())
F = P*((1+i))**N-P
print('利息={:.2f}'.format(F))

  Remember to subtract the principal to calculate interest.

8. Stores give change to customers

The store needs to give change to customers, and now there are only a few pieces of 50 yuan, 5 yuan and 1 yuan. Enter an integer amount value, and give a solution for changing money, assuming that there are enough RMB, and coins with large denominations are used first.

import math
money = (int)(input())
m50 = math.floor(money / 50)
money = money % 50  # 更新money
m5 = math.floor(money/5)
money = money%5
m1 = math.floor(money/1)
print("50yuan:",m50)
print("5yuan:",m5)
print("1yuan:",m1)

  Finally some additional knowledge. Here we are going to divide evenly, right \\? It is also rounded down, but if you want to round up. You need round(1.75) , a syntax similar to coercion.

9. Get month string

Input a number from 1 to 12, and output the corresponding month, for example, input 3, and output "March"

s = int(input())
b = ['0', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
print(b[s] + "月份")

  Of course, you can also replace it with a tuple, which is the best way. It is not difficult to learn this kind of index operation.

10. Count the number of English string words

Enter an English string, split words according to spaces and count the number of words

str = input().split(' ') # 
print(len(str)) # 获取长度

  Here we divide the input English sentence into words by spaces. Stored in the str list, use the len() function to directly find the length, that is to say the number of words.

11. Determine leap year

Enter a year to determine whether it is a leap year ‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬
The criteria for judging
1leap year , can be 4 divisible by, but not divisible by 100 ‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫
2. Divisible by 400

year = int(input())
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print(f'{year:}年是闰年')
else:
    print(f'{year:}年不是闰年')

12. Character replacement

Suppose there is a piece of English, in which a single letter "P" is mistakenly written as "p", please write a program to correct it.

str = input()
if 'p' in str:
    str = str.replace('p','P')
print(str)

  Remember here that there is a replace (target character, character to be replaced) method for string operations.

13. User login

A student's username and password are both abc123, judge whether the username and password entered by the student are correct, if correct, output "Welcome to enter", otherwise output "Incorrect, please re-enter!"

user = 'abc123'
pasword = 'abc123'
user1 = input()
password1 = input()
if user == user1 and pasword == password1 :
    print('欢迎进入')
else:
    print('有误,请重新输入!')

  It is a general judgment statement without difficulty.

14. Reverse an integer

Input a non-zero decimal integer (the existence of leading 0 is not allowed, that is, input like 0123 is not allowed), and it will be reversed and output.

x = input()
y = x[::-1].strip('0')  # 删除零
if y[-1] == '-':
    print('-'+y[:-1])
else:
    print(y)

  Here we input the integer of the string, use x[start address: end address: step size (-1 means to reverse the string)], and use the strip() method to get out 0 at both ends. If there is a negative number, the reversed negative The sign will appear at the end, let's output to control the output content: add a minus sign in front, and y should not output the minus sign.

15. Height measurement

Calculate the height in centimeters, the formula refers to the following: ‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪
Male height = (father's height + mother's height) × 1.08÷2 ‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭
women Height = (father's height × 0.923+ mother's height) ÷ 2 ‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪
Gender input "male" or "female", this question ensures that all test input height data are integers, and the output results are rounded . If the gender input does not meet the requirements, output "no corresponding formula"

dad = int(input())
mom = int(input())
child = input()
if child == '男':
    hight = int((dad + mom) * 1.08 / 2)
    print(hight)
elif child == '女' :
    hight = int((dad * 0.923 + mom) / 2)
    print(hight)
else:
    print("无对应公式")

  It is to carry out a branch processing formula according to the gender of the child, and the output will be fine. Note the case of non-human gender.

16. Inches and centimeters upgrade

Although our country has popularized the International System of Units very well, in real life, there are still many places that use imperial units to describe. For example, when we buy a 65-inch TV, the inch in this place refers to inches, or Take a 2 inch photo, using the same imperial units. ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬ ‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬
It is now

xin = input()
if xin[-1] == 'i' :
    xout = eval(xin[0:-1])*2.54
    print(f'{xout:.2f}cm')
elif xin[-4:] == 'inch':
    xout = eval(xin[0:-4])*2.54
    print(f'{xout:.2f}cm')
elif xin[-1]=='c':
    xout = eval(xin[0:-1])/2.54
    print(f'{xout:.2f}inch')
elif xin[-2:] == 'cm':
    xout = eval(xin[0:-2])/2.54
    print(f'{xout:.2f}inch')
else:
    print('输入错误。')

  This question mainly considers slices and conditional statements, and it is not difficult to take your time.

17. The power to go up every day

There are 365 days in a year, based on the ability value of the first day, recorded as 1.0. When studying hard, the ability value increases by N‰ compared to the previous day; when not studying, the ability value decreases by N‰ compared to the previous day due to forgetting and other reasons. If you work hard every day or let it go, what is the difference in ability value in a year? Wherein, the value range of N is 1 to 10, and N may be a decimal. ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬ ‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬
Obtain the user input N, calculate the ability value and the ratio between abilities after working hard every day and letting go for 365 days a day, where the ability value retains 2 decimal places, the ratio between abilities outputs an integer, and outputs Use the "comma+space" format between the results.

N=eval(input(""))
up=pow((1.0+0.001*N),364)
down=pow((1.0-0.001*N),364)
ud=int(up//down)
print("{:.2f}, {:.2f}, {}".format(up, down, ud))

  This question is the limit value, either a good student for a whole year, or a bad one for a whole year, what a loss. Pay attention to N%, this N represents the number in front of the percent sign, remember to shrink it when using it!

18. Determine the value type

The imaginary part of the complex number must have "j" or "J", and the floating point number must contain ".". Enter a number, please judge and enter whether the number is "complex number", "floating point number" or "integer". The title ensures that the input is a valid number.

str = input()
if 'j' in str or 'J' in str:
    print('复数')
elif '.' in str:
    print('浮点数')
else:
    print("整数")

19. Finding the root of a quadratic equation in one variable

For the quadratic equation ax2+bx+c=0, the values ​​of a, b, and c are entered by the user in three lines, and the real number solution of the equation is solved according to the values ​​entered by the user: ‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬ ‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
If the value of a is 0, judge whether the equation has a solution according to the value of b and output it, if a and b are both 0, then output Data error! ‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬
If the equation has no real solutions, output " The equation has no real solution"; ‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫
If the equation has two identical real number solutions, output one solution, and the result retains 2 decimal places; ‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫ ‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪
If the equation has two different real number solutions, output the two solutions of the equation in descending order in one line, separated by a space, and keep 2 decimal places in the result.

a = eval(input())
b = eval(input())
c = eval(input())
der = b*b - 4*a*c

if der < 0:
    print("该方程无实数解")
    exit(0)
if a == 0 and b == 0:
    print("Data error!")
    exit(0)
if a == 0:
    print(f'{-c/b:.2f}')
    exit(0)
if der == 0:
    print(f'{-b/(2*a):.2f}')
    exit(0)
else:
    print(f'{(-b+der**0.5)/(2*a):.2f} {(-b-der**0.5)/(2*a):.2f}')

  In this multi-branch situation, we can consider exit(0) one by one to meet the conditional output. It is very trouble-free.

20. Count the number of characters by category A

Write a program, the user enters a string, ends with a carriage return, and counts the number of English letters, numeric characters and other characters in the string (the carriage return represents the end and is not included in the statistics).

str1 = input()
letter = digit = other = 0
for c in str1:
    if c.isalpha():
        letter += 1
    elif c.isdigit():
        digit += 1
    else:
        other += 1
print(f'letter = {letter}, digit = {digit}, other = {other}')

  Here learn two judgment functions, isalpha(), isdigit(), to judge the character type. for c in str1 is to take the character c from str1 in a loop.

21. Determine the triangle and calculate the area

Input three numbers a, b, c, and judge whether they can form a triangle with the three side lengths. If yes, output YES and the area of ​​the triangle (reserve the result to 2 decimal places), otherwise output NO.

a = float(eval(input()))
b = float(eval(input()))
c = float(eval(input()))
s = (a+b+c)/2
if a+b>c and a+c>b and c+b>a:
    print("YES")
    print(f'{(s*(s-a)*(s-b)*(s-c))**0.5:.2f}')
else:
    print("NO")

  Here, Heron's formula is used for calculation, which may not be given in the exam, so it must be remembered . The judgment of a triangle is that the sum of two sides is greater than the third side.

22. Personal Tax Calculator

The current personal income tax calculation formula in China is as follows: ‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬
Personal income tax
Thepayable personal tax exemption amount is 5,000 yuan per month. After adjustment from October 1, 2018, that is, the 7-level progressive personal income tax rate table implemented in 2018 is as follows: ‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬ ‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬
Monthly taxable income (including tax brackets) Tax rate (%) Quick deduction
No more than 3,000 yuan3 0
Exceeding 3,000 yuan to 12,000 yuan10 210
Exceeding 12,000 yuan to 25,000 yuan Part of 20 1,410 Part
of over 25,000 to 35,000 25 2,660
Part of over 35,000 to 55,000 30 4,410
Part of over 55,000 to 80,000 35 7,160
The portion over $80,00045 15,160 ‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫
Please write a personal tax calculator. The user inputs the amount of five social insurances and one housing fund deducted from payable wages and salaries, and outputs the tax payable and actual income. When paying wages, the results are kept to two decimal places. When the input number is less than 0, output "error".

#个税计算器

n=eval(input())
x=0
if n<0:
    print('error')
else:
    if 0<=n<=5000:
        x=0
    elif 0<n-5000<=3000:
        x=(n-5000)*0.03
    elif 3000<n-5000<=12000:
        x=(n-5000)*0.1-210
    elif 12000<n-5000<=25000:
        x=(n-5000)*0.2-1410
    elif 25000<n-5000<=35000:
        x=(n-5000)*0.25-2660
    elif 35000<n-5000<=55000:
        x=(n-5000)*0.3-4410
    elif 55000<n-5000<=80000:
        x=(n-5000)*0.35-7160
    elif 80000<n-5000:
        x=(n-5000)*0.45-15160
    y=n-x
    print('应缴税款{:.2f}元,实发工资{:.2f}元。'.format(x,y))

  Pay attention to the calculation rules of this question. At each stage, you only need to subtract the principal multiplied by the weight, and then subtract a calculated tax.

23. Taxi metering

The charging standard of a taxi in a certain city is as follows:
the starting mileage is 3 kilometers (including 3 kilometers), the starting fee is 13 yuan; the
part of the part within 15 kilometers after the starting mileage is exceeded, the basic unit price is 2.3 yuan/km;
the passenger driving exceeds 15 kilometers For some parts, 50% of the basic unit price will be charged;
the slow-speed driving time below 12 km/h will be included in the waiting time, and an additional 1 yuan will be charged for every minute of waiting;
please enter the mileage (integer), waiting time, and output fare.

mile, wait = map(int, input().split(','))
if mile <= 3:
    print(f'{13 + wait:.0f}')

elif mile<= 15:
    print(f'{13 + (mile - 3) * 2.3 + wait:.0f}')
elif mile > 15:
    print(f'{13 + 12 * 2.3 + (mile - 15) * (1 + 0.5) * 2.3 + wait:.0f}')

  This kind of question is more about the understanding of the topic, and there is a ladder.

24. Body Mass Index BMI

BMI: Body Mass Index is an important standard commonly used in the world to measure human obesity and health, mainly used for statistical analysis ‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭ ‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮ ‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪
Definition : BMI = weight (kg) / height2 (m2) ‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪ ‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪ ‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮
Obtain the weight and height values ​​input by the user, calculate and give the international and domestic BMI classification

height,weight = map(float,eval(input))
BMI = weight/height
if BMI < 18.5:
    print(f'BMI数值为:{BMI:.2f}')
    print('BMI指标为:国际\'偏瘦\',国内\'偏瘦\'')

if 18.5<BMI < 25:
    print(f'BMI数值为:{BMI:.2f}')
    print('BMI指标为:国际\'正常\','+'')
if 18.5<BMI < 24:
        print("国内正常")
    
if 25<BMI < 30:
    print(f'BMI数值为:{BMI:.2f}')
    print('BMI指标为:国际\'偏胖\','+'')
if 24<BMI < 28:
        print("国内偏胖")
if 30<=BMI:
    print(f'BMI数值为:{BMI:.2f}')
    print('BMI指标为:国际\'肥胖\','+'')
if 28<=BMI:
        print("国内正常")

  We can combine according to the range. The international range is larger. First determine its index, and then we use a + string to link, and we will discuss it later in China. Note our use of double quotes \'.

25. Water bill calculation

设某单位收取水费的规定是:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
每月用水量w<=10吨时,每吨按0.32元计费;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
每月用水量w<=20吨时,超过10吨的部分每吨按0.64元计费,10吨以内仍然按每吨0.32元计费;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
每月用水量w>20吨时,超过20吨部分每吨按0.96元计费,20吨以内仍按原标准计费。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
编写程序根据用户输入的用水量w计算水费x并输出。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

water = eval(input())
if water <= 10:
    print(water*0.32)
elif water<= 20:
    print(3.2+(water-10)*0.64)
    # print(f'{32+(water-10)*0.64:.1f}')
else:
    print(f'{3.2+6.4+(water-20)*0.96:.1f}')

  就是一个阶梯计算,不解释。

26. 打折

某百货公司为了促销,采用购物打折的优惠办法。顾客一次购物折扣如下表所示。编写应用程序,由用户输入购物价值,计算并输出优惠价,结果保留1位小数。

money = eval(input())
if money <1000:
    print(f'折后价:{money:.1f}元。')
elif money < 2000:
    print(f'折后价:{money*0.95:.1f}元。')
elif money < 3000:
    print(f'折后价:{money*0.9:.1f}元。')
elif money < 5000:
    print(f'折后价:{money*0.85:.1f}元。')
else:
    print(f'折后价:{money*0.8:.1f}元。')

27. 求和

从键盘输入一行字符,将其中的数字求和并输出结果。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
输入格式:
输入一个行字符。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
输出格式:
若字符串中有数字则输出数字之和,若无数字则输出输入字符串中无数字,参看输入输出示例。

line = input()
num_count = 0
num = 0
for c in line:
    if c.isdigit():
        num_count+=int(c)
        num+=1
if num !=0:
    print(f'数字之和是{num_count}')
else:
    print('输入字符串中无数字')

  我们输入的是一个字符串,先用一个 for 循环对字符串每一个字符进行遍历,再用 isdigit() 函数进行判断。是数字我们就求和,不是我们不管,最后看 sum 是否为零,是表示没有数字,否则输出相应的和。

28. 中国古代数学问题——李白买酒

诗仙李白,一生好酒。一天,他提着酒壶,从家里出来,酒壶中有酒若干。他边走边唱:无事街上走,提壶去买酒,遇店加一倍,见花喝一斗,五遇店和花,喝光壶中酒,计算并输出壶中原有酒几斗?‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
要求使用循环来计算,不允许使用幂函数。

n = 0
for i in range(5):
    n+=1
    n=n/2
print(n)

  这里我们要注意了,五次遇到店和花。这两个是绑在一起的,开始我在这里都理解错了。

29. 人生苦短我用python

输入一个小于等于12的整数n,逐个输出字符串人生苦短我用python 中前n个字符,每个字符后输出一个半角逗号和一个空隔。

s = '人生苦短我用python'
for i in range(int(input())):
    print(s[i]+', ',end='')

  注意我们这种循环输出,end = ’ ',如果换成+链接字符串最后一个要报错,没有链接对象,你要改就复杂了,没有这个好。

30. 计算整数各位数字之和

输入一个正整数,计算其各个位的数字之和

count,str1 = 0,input()
for c in str1:
    if c.isdigit():
        count+=int(c)
print(count)

  这个与 27 很像,不讲了。

31. 阶乘求和

输入一个正整数n,计算 1!+2!+3!+…+n! 的和并输出。

n = int(input())
count,count1 = 0,1
for i in range(1, n + 1):
    for j in range(1, i + 1):
        count1 *= j
    count += count1
    count1 = 1
print(count)

  我们阶乘求和,先把一个阶乘表达出来,在对每一个阶乘求和,count1 负责阶乘即内循环,注意每一次内循环后记得归一,count就是总和,外循环。

32. 用For循环求100以内的素数

素数又称质数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。在一般领域,对正整数n,如果用2到 n-1之间的所有整数去除,均无法整除,则n为素数。(该算法可优化)
题目要求:求出100以内的所有素数并输出,素数之间以一个英文空格区分。

for i in range(2, 100 + 1):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        print(f'{i} ', end='')

  这里比 C 语言的求和舒服多了,只有一个限制条件,100以内的,而且是大于2的输入。我们只需要素数就好了。细心的同学会知道,这个 if else 结构不科学,严格来说 else 没有匹配对象。结果是对的,当然我们也可以这样:

for i in range(2, 100 + 1):
    flag = True # 代表是素数
    for j in range(2, i):
        if i % j == 0:
            flag=False
            break
    if flag:
        print(f'{i} ', end='')

33. 十进制整数转二进制

十进制整数转二进制的方法是:除以2,取出余数,商继续除以2,直到得到0为止,将取出的余数逆序即可得到对应的二进制数的各位。 例如:22转二进制的计算过程:
22 / 2 11 余0
11/2 5 余 1
5 /2 2 余 1
2 /2 1 余 0
1 /2 0 余 1
得到22的二进制是10110

number = int(input())
str1 = ''
if number==0:
    str1 = str1+ '0'
while number != 0:
    mod = number % 2
    str1 = str1 + f"{mod}"
    number = number // 2
print(str1[::-1])
  1. 获取输入
  2. 设置一个空串
  3. 条件控制:
    1. 输入是 0 ,二进制就是 0 .
    2. 不等于 0 ,莫 2 取余,添加到 str
  4. 倒序输出。

34. 用户登录的三次机会

给用户三次输入用户名和密码的机会,要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
如输入第一行输入用户名为 ‘Kate’,第二行输入密码为 ‘666666’,输出 ‘登录成功!’,退出程序;
当一共有 3 次输入用户名或密码不正确输出 “3次用户名或者密码均有误!退出程序。”。

set1={'Kate','666666'}
for i in range(3):
    user = input()
    keyworld = input()
    if user in set1 and keyworld in set1:
        print("登录成功!")
        exit(0)
print("3次用户名或者密码均有误!退出程序。")

  这里使用一个集合存放这两个答案,然后一个for 循环获取三次输入,输入在集合里面就输出 登陆成功 并退出。三次输出错误打印相应信息。其实程序的出口不要太多了,可是我就觉得只要答案对,编程简单就好了,特别是条件语句我就很喜欢这么弄 exit(0)。

35. 输入n,输出2*n-1行的菱形图形

insert image description here

print('   *   ')
print('  ***  ')
print(' ***** ')
print('*******')
print(' ***** ')
print('  ***  ')
print('   *   ')

这个是不动脑子的输出的,要是动一下脑子输出一个公式。可以这样:

 # 最大的三角形是 4 行,最长的一行有 7 个 *,也就是2n-1个位置
 # 我们需要把三角形看成两个元素组成,空格和*
 
 # 正三角
n = 4
for i in range(n+1):
    print(' '*(n-i)+'*'*(2*i-1))
#倒三角,第一行最长,空格和 * 一共六个元素也就是 2n
for i  in range (1,n):# n 取不到 4,但是要从第一行开始取了。
    print(' '*i+'*'*(2*n-1-2*i))

可以把n换成任意正整数,这是课堂正解。

36. 百钱买百鸡

我国古代数学家张丘建在《算经》一书中提出的数学问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,如果要求鸡翁、鸡母、鸡雏都不为零,问鸡翁、鸡母、鸡雏各几何。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
现在的问题是:用户输入鸡的数量和钱数,鸡翁、鸡母、鸡雏各为多少?如果有解,输出全部解,并按鸡翁数量由少到多的顺序输出;如果无解则输出“无解”。

num_chicken, money = map(int, input().split(' '))
cocks = 0  # 公鸡
hens = 0  # 母鸡
chick = 0		# 小鸡
count_chicken = 0 # 鸡的总数
count_money = 0		# 钱的总数
status = 0 # 状态控制,满足条件输出,否则就无解。
for i in range(1, num_chicken):

    for j in range(1,num_chicken):

        for k in range(1,num_chicken):
            count_money = 5 * i + 3 * j + k / 3
            count_chicken = i + j + k
            if count_chicken == num_chicken and count_money == money:
                status = 1
                print(f"{i} {j} {k}")
if status == 0:
    print("无解")

  这里凭借三个 for 循环,三种鸡的范围是 (1,n+1) 作为循环指标。在内循环计算鸡的数目,钱的数目,再来一个 if 判断,只要满足条件,就输出。这里不能使用exit(0),因为满足条件有很多组,所以使用一个 status 来控制输出。

37. 蒙特卡洛法

使用蒙特卡洛法求出曲线y=x*x与x轴之间在0-1范围内的面积‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
种子数为10‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
使用100000个点进行计算‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
结果保留3位小数

import random as rd 
rd.seed(10)
total = 100000
cnt = 0
for i in range(1,100000+1):
    x = rd.random()
    y = rd.random()
    if x**2>=y:
        cnt+=1 
print(f'{cnt/total:.3f}')

  首先学会随机数的生成,其次明白什么是蒙特卡洛法(不会就百度,一看就会)。下方 y < x*x ,输出一个比例。

38. 二分法求平方根

设计一个用二分法计算一个大于或等于 1 的实数 n 的平方根的函数sqrt_binary(n),计算精度控制在计算结果的平方与输入的误差不大于1e-6。

def Binsecond(n):
    low,up = 0,n 
    while True:
        mid = (low+up)/2
        if abs(mid*mid-n)<1e-6:
            return mid 
        elif mid*mid - n> 0:
            up = mid
        else:
            low = mid 
n = eval(input())
x = Binsecond(n)
print(x)
print(n**0.5)

  二分法两个指针嘛,把被开方数放在数轴末端,我们一开始折半查找,大了就向左边靠,小了往右边靠,直到我们相等为止。

39. 判断火车票座位

火车时速在200以上的列车:座位号是以ABCDF五个字母为区分的,一般会把座席分为商务座、一等座、二等座。
商务座:座位布局“2+1”,一排有三个座位,其中AC是相连的,F是单独一个座位;
一等座:座位布局“2+2”,一排有四个座位,其中AC是相连的,DF是相连的。
二等座:座位布局“3+2”,一排有五个座位,其中ABC是相连的三个座位,DF是相连的两个座位,这些座位中A、F都是靠窗的座位。
用户输入一个数字和一个字母组成的座位号,根据字母判断位置是窗口、中间还是过道。每个车厢座位排数是1-17,输入时不区分字母大小写。根据输入判定座位的位置,当输入的数据不是一个合法的座位号时,输出“座位号不存在”。

try:
    ticket = input()
    if 1<=eval(ticket[:-1])<=17:
        if ticket[-1] in ['A','a','F','f']:
            print("窗口")
        elif ticket[-1] in ['C','c','D','d']:
            print('过道')
        elif ticket[-1] in ['B','b']:
            print('中间')
        else:
            print('座位号不存在')
    else:		# 数字超出范围
        print('座位号不存在')
except:
    print('座位号不存在')

  我们主要从输入开始想办法,而且决定窗口还是哪里,我们把最后一个字符串的所有可能分类放在列表或者元组里面,再加以判断。使用 try 就是输出那些非法输入。

40. 计算圆周率

根据下面的泰勒级数关系式,依次累加绝对值不小于阈值的项,求圆周率的值。

n=eval(input())
pi=k=0
f=1
while abs(1 / (2 * k + 1))>=n:
    pi = pi + f * (1 / (2 * k + 1))
    k+=1
    f=-f
print("{:.6f}".format(4*pi))

  我们把 +,- 用一个 flag 来控制,而小数精度有最后一项来决定的,此时我们主要求出表达式就好,所幸题目最后一项带 k 的哪一项以经说了。

41. 找数字,做加法(升级版)

使用两个 input() 函数,接收用户的两个输入字符串,每个字符串最多包含一个英文点号“.”,将每个字符串中的数字和点号找出,按顺序组成一个数字。将得到的两个数字求和并输出,如果用户输入的引文点号“.”超过1个,提示输入错误,具体内容参阅以下示例。

x1 = input()
x2 = input()
s1,s2 = '',''
if x1.count('.') >1 or x2.count('.')>1:
    print('输入错误')
    exit(0)
for i in x1:
    # TODO: write code...
    if '0'<=i<='9' or i=='.':
        s1=s1+i
for i in x2:
    # TODO: write code...
    if '0'<=i<='9' or i=='.':
        s2=s2+i
print(eval(s1)+eval(s2))

统计字符个数 s.count ( ’ 统计字符 ’ )

  1. 获取输入 x1, x2,创建空串 s1, s2
  2. 判断小数点的个数,不满足打印提示信息,直接退出程序
  3. 满足就需要对字符串每个字符进行判断,提取出数字,两个字符串,所以循环遍历两遍。
  4. 输出两个字符串的和,使用eval()函数,把字符串转换成数字再相加。

42. 兔子繁殖问题

古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,用户输入一个月份数,计算并在一行内输出该月的兔子总对数以及前一个月与该月兔子数量的比值,比值结果保留小数点后3位,数字间用空格分隔。

n = int(input())
# f0代表前两月兔子,f1代表前一个月兔子,f代表本月兔子。
f0, f1 = 1, 1
f = f0 + f1
i = 3
while i < n:
    f0 = f1
    f1 = f
    f = f1 + f0
    i += 1
print(f'{f} {f1/f:0.03f}')

  这里我们需要明白题意:f0 代表前两月兔子,f1代表前一个月兔子,f 代表本月兔子。输入 n,兔子繁殖 n 后,求 f 以及 f1 / f 的比值,结果保留三位小数。题中隐含了你的月份必须从三月开始,两只没长大的兔子不能生小兔崽子。程序也就很简单:

  1. 获取输入n, 初始化 f0 = f1 = 1,f = f0 + f1 ,i = 3
  2. while 循环,新的一月,就更新f0,f1,f 的值。
  3. 输出

42. 身份证号校验

中国目前采用的是18位身份证号,其第7-10位数字是出生年,11-12位是出生月份,13-14是出生日期,第17位是性别,奇数为男性,偶数为女性,第18位是校验位。 如果身份证号码的其中一位填错了(包括最后一个校验位),则校验算法可以检测出来。如果身份证号的相邻2位填反了,则校验算法可以检测出来。校验规则如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。   
将这17位数字和系数相乘的结果相加。用加出来和除以11,看余数只可能是:0-1-2-3-4-5-6-7-8-9-10 分别对应的最后一位身份证的号码为:1-0-X-9-8-7-6-5-4-3-2   
通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的X(大写英文字母X)。如果余数是10,身份证的最后一位号码就是2。 用户输入一个身份证号,校验其是否是合法的身份证号码。

ID = input()
coe = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]  # 系数
sum = 0
for i in range(0, 17 ):
    sum += int(ID[i]) * coe[i]
rem = sum % 11  # 余数
if ID[17] == 'X':
    if rem == 2:
        print('身份证号码校验为合法号码!')
    else:
        print('身份证校验位错误!')
elif (rem + int(ID[17])) % 11 == 1:
    print('身份证号码校验为合法号码!')
else:
    print('身份证校验位错误!')

  这个就是一个流程。身份证前 17 位乘以一个系数用来加密计算,最后一位用来校验。校验方法解释一下,我们第取余数后,若为2最后一位必为x,其他结果都是把余数(求模后的结果)和身份证最后一位相加再模 11 结果为 1 才正确。

  1. 获取 18 位的身份证输入,字符类型。初始化一个系数列表(元组也可以)17 个元素。
  2. for 循环,前 17 位乘以系数。
  3. 模上 11,用来判断。
    1. 先判断 x 和 2这种比较简单 。
    2. 其他结果,加上身份证最后一位,模 11 恒等于 1 才正确,输出相应信息。

注意加减法需要进行强制转换。

43. 打印星号三角形

编写打印星号三角形函数,使用该函数,传入数据2、3、4打印出如下圣诞树图形

def santa(n):
    for i in range(1,n+1):
        print(' '*(4-i)+'*'*(2*i-1)) # 4 代表中心,每一行空格加星星
#上面的4-i可以根据数据进行修改
santa(2)
santa(3)
santa(4)

44. 判断IP地址合法性

互联网上的每台计算机都有一个独一无二的编号,称为IP地址,每个合法的IP地址由’.‘分隔开的4个数字组成,每个数字的取值范围是0-255。 现在用户输入一个字符串 s (不含空白符,不含前导0,如001直接输入1),请你判断 s 是否为合法IP,若是,输出’Yes’,否则输出’No’。 如用户输入为202.114.88.10, 则输出Yes; 当用户输入202.114.88,则输出No。

s = input()
def f(s):
	# 以“.”为标志对字符串进行切片,得到一个字符串列表
    lists = s.split('.')
    # 是否是4个数字
    if len(lists) != 4:
        return 'No'
    # 是否都在0-255之间
    for i in range(4):
        try:
            tmp = int(lists[i])
            if tmp not in range(0,256):
                return 'No'
        except:
            return 'No'
    return 'Yes'
print(f(s))
  1. 获取输入s,并编写函数对s进行判断
  2. 对字符串进行分割存放在 lists 中,判断步骤如下:
    2.1 首先满足长度为 4 才继续,否则返回 No.
    2.2 判断每个分区是否为数字,这里可以用 try 来强转,转不动说明非数字,输出No。
    2.3 判断数字的范围,满足 (0,255+1) 才输出 ‘ Yes’,否则输出 NO.

45. 编写闰年判断函数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

将判断闰年封装为函数,如果是闰年,函数输出True,否则输出False.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
使用该函数输出2010-2020年之间所有闰年

def judge():
    for i in range(2010,2020+1):
        # TODO: write code...
        if i%4==0 and i%100!=0 or i%400==0:
            print(i)
judge();

46. 校验身份证号码并输出个人信息

中国目前采用的是18位身份证号,其第7-10位数字是出生年,11-12位是出生月份,13-14是出生日期,第17位是性别,奇数为男性,偶数为女性,第18位是校验位。
如果身份证号码的其中一位填错了(包括最后一个校验位),则校验算法可以检测出来。如果身份证号的相邻2位填反了,则校验算法可以检测出来。校验规则如下:

  1. 将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
  2. 将这17位数字和系数相乘的结果相加。
  3. 用加出来和除以11,看余数只可能是:0-1-2-3-4-6-7-8-9-10
    分别对应的最后一位身份证的号码为:1-0-X-9-8-7-6-5-4-3-2
  4. 通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的X(大写英文字母X)。如果余数是10,身份证的最后一位号码就是2。
    用户输入一个身份证号,校验其是否是合法的身份证号码,如身份证号码不合法输出 ‘身份证校验位错误!’,如身份证号码合法则分别在4行中输出’身份证号码校验为合法号码!',并输出该人的出生年月日和性别。
s = input()
def printID(s):
    print('身份证号码校验为合法号码!')
    print(f'出生:{s[6:9 + 1]}{s[10:11 + 1]}{s[12:13 + 1]}日')
    if int(s[16]) % 2 == 0:
        print('性别:女')
    else:
        print('性别:男')
def ID(s):
    mod = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    sum = 0
    for i in range(0, 17):
        sum += int(s[i]) * mod[i]
    ref = sum % 11
    if ref == 2 and s[17] == 'X':
        printID(s)
    elif (ref + int(s[17])) % 11 == 1:
        printID(s)
    else:
        print('身份证校验位错误!')
ID(s)

这类题已经出现过很多次了,如果你们的学校经常出这类题。请你重视,此题换汤不换药,只是输出的类容不同,所以我们只需要在编写一个打印函数就好了。

47. 哥德巴赫猜想

数学领域著名的“哥德巴赫猜想”的大致意思是:任何一个大于2的偶数总能表示为两个素数之和。比如:24=5+19,其中5和19都是素数。本实验的任务是设计一个程序,验证20亿以内的偶数都可以分解成两个素数之和。输入一个大于2的正整数,当输入为偶数时,在一行中按照格式“N = p + q”输出N的素数分解,其中p 、 q均为素数且p ≤ q。因为这样的分解可能不唯一(例如24还可以分解为7+17),要求必须输出所有解中p最小的解。当输入为奇数时,输出’Data error!’ 。

def Isprime(n):
    for i in range (2,n):
        if n%i == 0:
            return False
    return True
    
N = int(input())
if N %2 ==1:
    print('Data error!')
else:
    for i in range (2,N-2+1):
        if Isprime(i) and Isprime(N-i):
            print(f'N = {i} + {N-i}')
            break
  1. 一个判断数字是否为素数的函数(这里没有PTA上的那么复杂,简单判断一下就好)。
  2. 获取输入,首先判断其是否为偶数,不是偶数,输出对应的错误信息。
  3. 一个循环搞定哥德巴赫猜想,都为同时判断是否为素数,输出相应信息。

48. 判断素数函数

写一个函数isPrime(n)用于判断一个数字n是不是素数,用户输入一个正整数,在一行内输出不大于该数的所有素数,各数后面用一个空格分隔。

def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n ** 0.5 + 1)):
        if n % i == 0:
            return False
    return True
    
n = int(input())
for i in range(2, n+1):
    if is_prime(i):
        print(f'{i}' + ' ', end='')

49. 汉诺塔

汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
柱子编号为a, b, c,将所有圆盘从a移到c可以描述为: 如果a只有一个圆盘,可以直接移动到c; 如果a有N个圆盘,可以看成a有1个圆盘(底盘) + (N-1)个圆盘,首先需要把 (N-1) 个圆盘移动到 b,然后,将 a的最后一个圆盘移动到c,再将b的(N-1)个圆盘移动到c。 请编写一个函数move(n, a, b, c) ,给定输入 n, a, b, c,打印出移动的步骤: 例如,输入 move (2, ‘A’, ‘B’, ‘C’),打印出: A –> B A –> C B –> C

def move(n, position, mid, aim):  # pos代表位置
    if n == 1:
        print(position, '-->', aim) # 最后一个饼移到目的地
    else:
        move(n-1,position, aim, mid) # 倒数第二块跑到中间构建中间的柱子
        print( position, '-->', aim) # 最后一块饼是从开始的柱子移动过去
        move(n-1,mid, position, aim) # 中间过程,中间就是最开始的柱子,我们目的不会改变


n = int(input())
a, b, c = map(str, input().split())
move(n, a, b, c)

本题理解逻辑有点苦难,是锻炼的机会。但是不明白也没事,记住三点:1.就是打印的地方 2.打印的内容一致。3.函数内嵌两个move(n-1,1,3,2) move(n-1,2,1,3)。

50.进制转换-递归

编写函数:把任意十进制整数n转换成k进制数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
输入输出示例:
输入n和k的值,‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬n表示需要转换的数据,‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬k为需要转换为的进制

def zhuan(n,k):
    ans = '' # 创建一个空串,用于转转化后的内容
    while n/k != 0: # 非整数除法,n==0停止
        ans = str(n%k) + ans  # 把余数存起来注意强制转换,还是倒起存的。
        n = n // k # 整除,当 n < K 除就为零。
    return ans
n,k = map(int,input().split(',')) # 把输入分割成两个整数
print(zhuan(n,k))

51. 最大公约数计算-递归

#00310030003700360038003600341668150999997
使用递归编写函数计算两个数据m和n的最大公约数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
算法:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
1.对于已知两数m,n,使得m>n;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
2.m除以n得余数r;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
3.若r=0,则n为最大公约数结束;否则执行步骤4;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
4.m ← n,n ← r,再重复执行步骤2。

m,n = map(int,input().split(',')) # 获取输入
def Gcd(m,n):
    r=m%n
    while r!=0:
        m=n # 这句话的意思,最大的数丢弃,把输入较小的数作为大数,取模后的余数作为小数,继续大树模上小数。
        n=r
        r=m%n
    print(n) # 找到最大公约数,打印。
Gcd(m,n)

52.数字统计

接收用户输入的多个数据,直到输入为空时停止‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
将用户输入的一组数据放入列表中存放‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
编写对列表进行统计的函数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
包括:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
计算列表的平均数,并返回数据‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
计算列表中的方差,并返回数据‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
计算列表中的中位数,并返回数据‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
使用这些函数和python自带的len()函数输出‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
1 列表中数据的个数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
2 列表数据和和‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
3 列表数据的平均值‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
4 列表数据的方差‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
5 列表的中位数

#请在此行下方编写函数
def getNum(): # 注意这个循环输入,输入为空返回列表 
    ls = []
    while True:
        num = input()
        if num:
            ls.append(eval(num))
        else:
            break
    return ls
    
def avg(ls): #求平均值
    s = 0
    for i in ls:
        s += i
    return s / len(ls)
def dev(ls): # 求方差
    a = avg(ls)  # 第一个函数求出平均值
    s = 0
    for  i in ls:
        s += (i-a)**2
    return s/len(ls)
    
def median(ls): #中位数
    ls.sort()
    if len(ls)%2 != 0: # 奇数直接在中间
        return ls[len(ls)//2]
    else: # 偶数,中间两位数减半
        return (ls[len(ls)//2]+ls[len(ls)//2-1])/2
ls=getNum()
print("数量",len(ls))
print("和",sum(ls)) # 直接求和
print("平均值",avg(ls))
print("方差",dev(ls))
print("中位数",median(ls))

  对公式熟练,循环输入,列表的用法。

53.绩点计算

平均绩点计算方法:(课程学分1绩点+课程学分2绩点+…+课程学分n*绩点)/(课程学分1+课程学分2+…+课程学分n) 用户循环输入五分制成绩和课程学分,题目测试数据保证至少有一组或以上合法数据输入。输入‘-1’时结束输入,计算学生平均绩点。

score = {'A': 4.0, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.5, 'D': 1.3, 'D-': 1.0,
         'F': 0.0} # 字典
gpaSum, creditSum = 0, 0
while True: # 控制输入
    s = input() # A,B,C这种等级
    if s == '-1':
        break
    elif s in score.keys():
        credit = float(input()) # 获取学分数
        gpaSum = gpaSum + score[s] * credit #
    else:
        print('data error') # D之外的输出错误信息
    creditSum = creditSum + credit # 课程学分求和
gpaAve = gpaSum / creditSum  # 求出结果
print('{:.2f}'.format(gpaAve))

54. 用户登录

有字典如下: dic = {‘admin’:‘123456’,‘administrator’:‘12345678’,‘root’:‘password’}‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
实现用户输入用户名和密码,当用户名与密码和字典中的键值对匹配时,显示“登录成功”,否则显示“登录失败”,登录失败时允许重复输入三次。

dic = {'admin':'123456','administrator':'12345678','root':'pasword'} # 字典,名字就是键,密码就是值
count = 0
while count<3: # 控制输入
    name = input()
    password = input()
    if name in dic.keys(): # 先检查名字是否满足键
        if password == dic[name]: # 检查密码是否满足
            print('登录成功')
            exit(0) # 满足退出
        else: # 密码错误,次数加一(共三次)
            count+=1
            print('登录失败')
    else: # 名字不满足键,次数加一
        count+=1
        print('登录失败')

55. 购物车

要求用户输入总资产,例如: 20000,输出所有商品序号和商品列表,每行一种商品,让用户根据序号选择商品,加入购物车购买,如果商品总额大于总资产,提示’账户余额不足,先去赚钱吧!‘,否则,输出’恭喜你成功购买一个某商品’

goods=[{"name":"电脑","price":4999},
{"name":"鼠标","price":80},
{"name":"游艇","price":200000},
{"name":"别墅","price":2000000}] # 组建一个列表字典
allmoney=int(input('')) # 钱的总数
for n, i in enumerate(goods): # n 负责标好,循环就是goods内容
    print(str(n)+' '+i['name'])
num=int(input()) #  选择购买的标号
if goods[num]['price']<=allmoney: # 钱够就可以购买
    print('恭喜你成功购买一个{}'.format(goods[num]['name']))
else:
    print('账户余额不足,先去赚钱吧!')

goods[列表的成员] [成员里卖弄的具体特征]。第一个 [ ] 锁定字典,[ ] 锁定字典的成员。

56. 身份证升位

第二代居民身份证是依据2003年6月28日第十届全国人大常委会第3次会议通过的《中华人民共和国居民身份证法》实施的。第一代身份证十五位数升为第二代身份证十八位数的一般规则是:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
第一步,在原十五位数身份证的第六位数后面插入19 (1905年1月1日以后出生)或20(2000.1.1-2004.12.31出生),这样身份证号码即为十七位数; 第二步,按照国家规定的统一公式计算出第十八位数,作为校验码放在第二代身份证的尾号。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
校验码计算方法:将身份证前十七位数分别乘以不同系数并求和 S = Sum(Ai * Wi) Ai:表示第i位置上的身份证号码数字值, i = 0, … , 16 Wi:表示第i位置上的加权因子,Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 S对11取模得到余数0-10,对应的校验码如下: 余数 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2

ID = input() # 获取ID
Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 17位权重
we18=[1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2] # 校验位
if int(ID[6:7+1]) in range(0,5):
    ID = ID[0:6]+'20'+ID[6:] # 进行拼接
else:
    ID = ID[0:6]+'19'+ID[6:] 
sum = 0 
for i in range(0,17):
    sum+=int(ID[i])*Wi[i]
qumo = sum%11
ID = ID + str(we18[qumo]) # 确定校验位
print(ID)

57. 统计次数

输入一个中文句子,以回车结束。统计并输出句子中字符个数及中文词数,要求中文分词采用jieba库中的lcut()函数进行。

import jieba
s = input()
print(f'中文字符数为{len(s)},中文词语数为{len(jieba.lcut(s))}。')


Category of website: technical article > Blog

Author:cindy

link:http://www.pythonblackhole.com/blog/article/25334/796e5f00638b1353de0d/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

30 0
collect article
collected

Comment content: (supports up to 255 characters)