Some simple exercises for python

Python beginners’ exercises to get started

Question: (Convert Celsius to Fahrenheit) The user inputs a number and converts it from Celsius to Fahrenheit.

Conversion formula: Fahrenheit = ( 9 / 5 ) * Celsius + 32

code show as below:

cel = float(input("请输入一个温度:"))
f = (9/5) * cel + 32
print("%s摄氏度对应的是%s华氏度"%(cel,f))



Topic: (Calculating the volume of a cylinder) Write a program that reads the radius and height of a cylinder and uses the following formulas to calculate the base area and volume of the cylinder.

*Calculation formula: area = radius * radius * 3.14

volume= area * length

code show as below:

r = int(input("请输入一个半径:"))
h = int(input("请输入一个高:"))
area =r * r * 3.14
volume = area * h
print("半径为%s对应的面积为%s"%(r,area))
print("半径为%s高为%s对应的体积为%s"%(r,h,volume))



Question: (Sum of the digits of an integer) Write a program to read an integer between 0 and 1000 and calculate the sum of its digits, for example, 932, the sum of each digit is 14.

Tip: Use % to extract numbers, and use the // operator to remove the extracted numbers (932%10=2 and 932//10=93)

code show as below:

a = int(input("请输入一个0~1000以内的数:"))
b = (a // 10)%10
c = (a // 100)%10
sum = b + c
print("各位数相加的结果为:%s"%(sum))



Topic: (Calculate the number of years and days) Write a program that prompts the user to enter the number of minutes, then converts the minutes into the corresponding number of years and days and displays the program. (A year is 365 days)

code show as below:

min =float(input("请输入分钟数:"))
day = min / 60 / 24 
print("%s分钟对应的天数为%s"%(min,day))
year = min / 60 / 24 / 365
print("%s分钟对应的年数为%s"%(min,year))



Topic: (Calculating Energy) Write a program to calculate the energy required to heat water from the initial temperature to the final temperature. Your program should prompt the user to enter the amount of water, the initial temperature, and the final temperature.

Calculation formula for heat: Q = (finalTemperature - initialTemperature) * 4184

code show as below:

m = float(input("请输入千克对应的水量:"))
ft = float(input("请输入最终温度:"))
it = float(input("请输入起始温度:"))
Q = m * (ft - it) * 4184
print("%s千克水量%s起始温度%s最终温度对应的热量为%s"%(m,it,ft,Q))



Question: (Wind Chill Temperature) Is it cold outside? Temperature values ​​alone are not enough to provide the answer. Other factors, such as wind speed, relative humidity, and lighting, have a great impact on how cold it is outside. In 2001, the National Weather Service (NWS) ) implements a new method of measuring wind chill using temperature and wind speed. Write a program that prompts the user to enter a temperature between -58 degrees Fahrenheit and 41 degrees Fahrenheit and a wind speed of 2 miles per hour or greater, and then displays the wind chill temperature.

Calculation formula: Twc = 35.74 = 0.6215 * t - 35.75 * v + 0.4275 * t * v

code show as below:

a = float(input("请输入一个-58~41之间的华氏度温度:"))
b = float(input("请输入一个大于等于每小时两里的风速:"))
c = 35.74 + 0.6215 * a -35.75 * b + 0.4275 * a * b
print("%s华氏度%s风速对应的风寒温度为%s"%(a,b,c))



Question: (Calculate runway length) Assuming that the acceleration a and take-off speed v of the aircraft are given, the shortest runway length required for the aircraft to take off can be calculated according to the following formula.

Calculation formula: length = v 2 / 2 *a

code show as below:

a = float(input("请给一个加速度为:"))
v = float(input("请给一个起飞速度为:"))
lengh = (v * v) / (2 * a)
print("%s加速度%s起飞速度对应的跑道长度为%s"%(a,v,lengh))



Topic: (Split numbers) Write a program that prompts the user to enter a four-digit integer and displays it in reverse order.

Example: Enter an integer : 5467
7
6
4
5

code show as below:

a = int(input("请输入一个四位整数:"))
print(a%10 )
print(a//10%10)
print(a//100%10 )
print(a//1000%10 )



Question: (Area of ​​a regular hexagon) Write a program that prompts the user to enter the side length of a regular hexagon and displays its area.

The formula for calculating the area of ​​a regular hexagon is: 3 * 3 \sqrt{3}3 *s 2 /2

code show as below:

import math
s = int(input("请输入边长的大小:"))
area = 3 * 3**0.5 / 2 * s * s
print("%s对应的面积为%s"%(s,area))



Question: (Area of ​​a Triangle) Write a program that prompts the user to enter the three vertices of a triangle (x 1 , y 1 ), (x 2 , y 2 ) (x 3 , y 3 ) and then displays its area.

Formula to calculate the area of ​​a triangle:

s = (sidel + sidel2 + sidel3) / 2
area = s ( s − s i d e l 1 ) ( s − s i d e l 2 ) ( s − s i d e l 3 ) \sqrt{s(s - sidel1)(s - sidel2)(s - sidel3)} s(ssidel1)(ssidel2)(ss i d e l 3 )

code show as below:

x1 = float(input("请输入第一个顶点的X"))
y1 = float(input("请输入第一个顶点的y"))
x2 = float(input("请输入第二个顶点的X"))
y2 = float(input("请输入第二个顶点的y"))
x3 = float(input("请输入第三个顶点的X"))
y3 = float(input("请输入第三个顶点的y"))
s1 = ((x2-x1)**2 + (y2-y1)**2 )**0.5
s2 = ((x2-x3)**2 + (y2-y3)**2 )**0.5
s3 = ((x1-x3)**2 + (y1-y3)**2 )**0.5
s  = (s1 + s2 +s3) / 2
area = (s * (s - s1)*(s - s2)*(s- s3))**0.5
print("三个顶点所形成三角形的面积为:" +str(area))

Thanks for watching!

Guess you like

Origin blog.csdn.net/Zombie_QP/article/details/123776076