python-programming training questions

1.1

Write a program to calculate the total amount of books purchased: the user enters the price of the book and the quantity of books purchased, and saves them in a variable of type float and an int respectively, and then calculates the total amount of books purchased according to the price entered by the user and the quantity of books purchased amount and output. Among them, the book sales strategy is: under normal circumstances, it will be sold at a 10% discount, if the number of books purchased exceeds 10, it will be discounted at 15%, and if the number of books purchased exceeds 100, it will be discounted at 20%

num = int(input('请输入购买书的数量:'))  # 书的数量
price = float(input('请输入购买书的价格:'))  # 书的价格
if 10 < num <= 100:
    print((num * price) * 0.85)
elif num > 100:
    print((num * price) * 0.8)
else:
    print((num * price) * 0.9)

insert image description here

1.2
The zoo wants to build a new triangular artificial lake, one is for the beauty of fish farming, and the other is to recycle water resources. Input the side lengths of the three sides A, B, and C from the keyboard, and please program to judge whether a triangle can be formed. (Elements that can form a triangle, the sum of the two sides is greater than the third side, and the difference between the two sides is less than the third side)
Requirements: If the three side lengths A, B, and C can form a triangle, output YES, otherwise NO.

a = float(input('请输入A边:'))
b = float(input('请输入B边:'))
c = float(input('请输入C边:'))
if a + b > c and a + c > b and b + c > a:
    print('YES')
else:
    print('NO')

1.3
Store A plans to start selling watermelons this summer. The prices of watermelons are as follows: 0.85 yuan per catty for more than 20 catties; 0.95 yuan per catty for those weighing more than 5 catties but less than or equal to 10 catties, 1.00 yuan per catty for those weighing less than or equal to 5 catties, 1.05 yuan per catty for those weighing less than or equal to 5 catties. Now, in order to know whether the store will be profitable, company A is required to help design a program that inputs the weight of the watermelon and the amount paid by the customer, and outputs the payable amount and the amount of change.

weight = float(input('请输入西瓜的重量:'))
price = float(input('顾客所付钱数:'))
repay = 0
money = 0
if weight > 20:
    money = weight * 0.85
elif 15 < weight <= 20:
    money = weight * 0.9
elif 10 < weight <= 15:
    money = weight * 0.95
elif 5 < weight <= 10:
    money = weight * 1
else:
    money = weight * 1.5
repay = price - money
if repay < 0:
    print(f'商品的价格是{
      
      money}元')
    print(f'你所支付的钱不够,你还需支付{
      
      -repay}元')
elif repay == 0:
    print('你所支付的钱刚刚好')
else:
    print(f'商品的价格是{
      
      money}元')
    print(f'找零{
      
      repay}元')

insert image description here

1.4
Statistical and data analysis of students' grades can find out students' mastery of knowledge, so that teachers can adjust teaching content and key and difficult points according to the results of the analysis. Now the following tasks need to be completed to realize the grade analysis system.
Input a percentile score t, convert it into the corresponding grade and then output it. The specific conversion rules are as follows: 90~100 is A,
80~89 is B,
70~79 is C,
60~69 is D,
0~59 is E
Requirements: If The input data is not within the range of 0~100, please output a line of error prompt: "Data out of range!"

grade = float(input('请输入成绩:'))
if 90 <= grade < 100:
    print('A')
elif 80 <= grade < 90:
    print('B')
elif 70 <= grade < 80:
    print('C')
elif 60 <= grade < 70:
    print('D')
else:
    print('E')

insert image description here

1.5
Receive an eleven-digit number from the keyboard, and judge whether it is a mobile phone number with 5 consecutive numbers (the last 5 numbers are the same). Rules: The first digit is 1,
the second digit can be one of the numbers 358, the following 4 digits are any number, and the last 5 digits are any same number. For example:
18601088888, 13912366666 are satisfied.
Note: output "false" if it is not satisfied, and "true" if it meets the requirements.

phone = int(input('请输入手机号码:'))
one = phone // 10000000000  # 第一位
two = phone // 1000000000 % 10  # 第二位
last_five = [phone % 10, phone % 100 // 10, phone % 1000 // 100, phone % 10000 // 1000,
             phone % 100000 // 10000]  # 最后五位
if one == 1:  # 第 1 位是 1
    if two == 3 or two == 5 or two == 8:  # 第二位可以是数字 358 其中之一
        if sum(last_five) == last_five[0] * 5:  # 最后5位为任意相同的数字
            print("true")
        else:
            print("false")
    else:
        print("false")
else:
    print("false")

insert image description here

1.6
Input the side length (a) of the square, the length (l) and width (d) of the rectangle, and the radius (r) of the circle in order, calculate and compare which of them has a larger area, and output the graphic with the largest area.
For example: input 1 3 4 1, output: rectangle

a, l, d, r = (input('按顺序输入正方形的边长(a),长方形的长(l)和宽(d),以及圆的半径(r):')).split(' ')
a = float(a)
l = float(l)
d = float(d)
r = float(r)
area = [a * a, l * d, r ** 2 * 3.14]
graphics = ['正方形', '长方形', '圆形']
print(graphics[area.index(max(area))])

insert image description here

1.7
The divisibility judgment game can significantly improve children's logical thinking ability. The requirements of the questions are as follows:
• Can be divisible by 3, 5, 7 at the same time
• Can be divisible by 3, 5 at the same time
• Can be divisible by 3, 7 at the same time
• Can be divisible by 5, 7 at the same time Divisibility
• Can only be divisible by one of 3, 5, 7
• Cannot be divisible by any of 3, 5, 7
Input an integer and output the result that meets the corresponding conditions. Requirement: Implemented using a branch structure statement.

data = int(input('请输入一个整数:'))
if data % 3 == 0 and data % 5 == 0 and data % 7 == 0:
    print('能同时被 3、5、7 整除')
elif data % 3 == 0 and data % 5 == 0:
    print('能同时被 3、5 整除')
elif data % 3 == 0 and data % 7 == 0:
    print('能同时被 3、7 整除')
elif data % 5 == 0 and data % 7 == 0:
    print('能同时被 5、7 整除')
elif data % 5 == 0 or data % 7 == 0 or data % 3 == 0:
    print('只能被 3、5、7 中的一个整除')
else:
    print('不能被 3、5、7 任一个整除')

insert image description here

1.8
Determine whether an integer is a "daffodil number". The so-called "narcissus number" refers to a three-digit integer whose cube sum of each individual number is equal to the number itself. For example: 153 is a "daffodil number", such as 153=1 3+5 3+3**3.
If it is the number of daffodils, output YES, otherwise output No

data = int(input('请输入一个三位的整数:'))
if data == (data // 100) ** 3 + (data // 10 % 10) ** 3 + (data % 10) ** 3:
    print('YES')
else:
    print('NO')

insert image description here

loop (contains for while list dictionary knowledge)

1.1
Accept a positive integer from the keyboard, and list the Chinese representation format of the number, for example: keyboard input 123, print out 123; keyboard input 3103, print out 3103. (Test loop and list index use

data = int(input('请输入一个正整数:'))
cn_num = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
print_answer = []
while data:
    print_answer.append(cn_num[data % 10])
    data //= 10
for i in range(len(print_answer) - 1, -1, -1):
    print(print_answer[i], end='')

insert image description here

1.2
The serial number of the ticket must be a subsequence of the total sequence in the system, please check the authenticity of the ticket.
Receive two strings a and b from the keyboard, please judge whether the string a contains the string b, if yes, output "Yes", otherwise output "No". There are multiple groups of test cases, each test case occupies a line, and two strings are separated by spaces.
For example: input JavaStudy Java, then output Yes Student School, then output No
Note: Please use a loop to judge whether the latter exists and the former.

a, b = input('按顺序输出字符串a,b:').split(' ')
for i in range(len(a) - len(b) + 1):
    if b == a[i:i + len(b)]:
        print('YES')
        break
else:
    print('NO')

insert image description here

1.3 Chinese knot
The company now needs to print the main knot of the Chinese knot (the largest knot in the middle). In order to print a beautiful and novel main knot, the length of the main knot is designed to be divisible by 7.
Now the company needs to count the number of integers divisible by 7 within a certain range , and the sum of these numbers divisible by 7.
Input an integer N from the keyboard, output the number of integers between 1 and N that can be divisible by 7, and the sum of these numbers that can be divisible by 7

sum_seven = 0
data = int(input('请输入一个正整数N:'))
for i in range(1, data):
    if i % 7 == 0:
        sum_seven += i
        print(i, end=' ')
print(f'\n被 7 整除的数的和:{
      
      sum_seven}')

insert image description here

1.4 Sequence summation
There is a sequence of fractions: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13 ... Find the sum of the first 20 items of this sequence. Requirements: Calculate the sum of the sequence using a loop. Note the changes in the numerator and denominator.
Note:
a1=2, b1=1, c1=a1/b1; a2=a1+b1, b2=a1, c2=a2/b2; a3=a2+b2, b3=a2, c3=a3/b3;

s = c1+c2+…+c20;
s is the sum of the first 20 items of the score sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13 ….


num_mom = 2  # 分母
num_sun = 1  # 分子
num_sum = 0  # 序列之和
temp = 0  # 替换值
for i in range(20):
    num_sum += num_sun / num_mom
    temp = num_mom
    num_mom = num_sun
    num_sun = num_sun + temp
print(num_sum)

insert image description here

1.5
A ball falls freely from a height of 100 meters, and bounces back to half of the original height after each landing, falls again, and bounces again. How many meters did the ball travel when it hit the ground for the tenth time? How high was the tenth rebound?

sum_h = 0
h = 100
for i in range(10):
    sum_h += h#相加下落的高度
    h = h / 2
    sum_h += h#相加反弹的高度
print(f'球经过{
      
      sum_h-h}米')
print(f'第十次反弹{
      
      h}米高')

1.6
Find an odd number and
write a program to realize it: input a positive integer s from the keyboard, take out the odd-numbered numbers in s from the low bit, and form a new number t in turn, the high bit is still placed in the high bit, the low bit is still placed in the low bit, and finally on the screen output t. For example, when the number in s is 7654321, the number in t is 7531.

num = int(input('输入一个正整数:'))
i = 1  # 循环次数判断奇、偶
remainder = 0  # 余数
t = 0
mul = 1  # 乘数
if num / 10 == 0:
    t = num % 10
else:
    while num:
        if i % 2 != 0:
            t += num % 10 * mul
            mul = mul * 10
        num = num // 10
        i += 1
print(f't的值是{
      
      t}')

insert image description here

1.7
In a parking lot, there are 48 cars and motorcycles parked. Each car has 4 wheels, and each motorcycle has
3 wheels. These cars have a total of 172 wheels. There are cars and motorcycles in the programming output parking lot. number of motorcycles.

i = 0  # 汽车数量
for i in range(1, 49):
    if i * 4 + (48 - i) * 3==172:
        print(f"汽车数量是:{
      
      i},摩托车数量是:{
      
      48-i}")

insert image description here

1.8
Xiao Ming participated in the "Campus Singer Contest" today. The judges' scoring rule is to calculate the average score after removing the lowest score and the highest score. Can you help Xiao Ming quickly calculate the average score? (The number of judges must be greater than 2)
Input instructions: first enter an integer n, representing the number of judges, and then enter n numbers. Please calculate the average score according to the calculation rules of the question and then output it.
For example, input: 6
100 90 90 80 85 95
Calculate the average score according to the title and output: 90.0

在这里插入代码片n = int(input('输入评委人数:'))
point = input('输入分数:').split(' ')
point = list(map(int, point))  # 将列表值转化为整形
point.remove(max(point))
point.remove(min(point))
print(f'平均分为:{
      
      sum(point) / len(point)}')

insert image description here

1.9 nine-nine multiplication table,
select multiplication formula mnemonic function, output ladder form 9*9 multiplication formula table

for i in range(1, 10):
    for j in range(1, i+1):
        print(f'{
      
      i}X{
      
      j}={
      
      i * j}', end=' ')
    print()

insert image description here

1.10
Enter a string to count the number of times each character appears in the string.

char = input('请输入一个字符串:')
data = []
count = []
for i in char:
    if i in data:
        count[data.index(i)] += 1
    else:
        data.append(i)
        count.append(1)
for i in range(len(data)):
    print(f'{
      
      data[i]}:{
      
      count[i]}次')

insert image description here

Guess you like

Origin blog.csdn.net/weixin_50804299/article/details/129349472