Python练习题 基本语法(1-18)一

Demo01
**

(将摄氏温度转化为华氏温度)编写–个从控制台读取摄氏温度并将它转变为华氏温度并予以显示 的程序。转换公式如下所示。. fahrenheit = (9 / 5) * celsius + 32 这里是这个程序的示例运行。 Enter a degree in Celsius: 43 F Ener 43 Ce1sius is 109.4 Fahrenheit

**

解答:
程序编辑:

# 数据 华氏温度,摄氏温度
# 指令:
# 1.提示用户输入摄氏温度
# 2.利用公式 将摄氏温度转化为华氏温度
# 3.输出两者的值

cel = float(input("Enter a degree in Celsius:"))
fah = (9 / 5) * cel + 32
print("%.0f Celsius is %.1f Fahrenheit" % (cel,fah))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day001.py
Enter a degree in Celsius:43
43 Celsius is 109.4 Fahrenheit

Process finished with exit code 0

在这里插入图片描述

Demo02
**

(计算圆柱体的体积)编写一个读取圆柱的半径和高并利用下面的公式计算圆柱体底面积和体积的 程序:

**
area = radius ★radius ★π
vo1ume = area * 1ength
这里是示例运行。
Enter the radius and 1ength of a cylinder: 5.5, 12 F B
The area is 95.0331
The volume is 1140.4
程序编辑:

# 数据:半径  ,高
# 指令:
# 1.提示输入圆柱的半径和高,定义PI=3.141592653
# 2.利用公式将圆柱体底面积和体积计算出来
# 3.将圆柱底面积和体积打印出来

radius,length = eval( input("请分别输入圆柱的半径和高(注意用“,”号隔开):" ) )
PI = 3.141592653
area = radius * radius * PI
volume = area * length
print("圆柱底面积为:%.4f\n体积为:%.1f"%(area ,volume ))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day02.py
请分别输入圆柱的半径和高(注意用“,”号隔开):5.5,12
圆柱底面积为:95.0332
体积为:1140.4

Process finished with exit code 0

在这里插入图片描述

Demo03
**

(对一个整数中的各位数字求和)编写一个程序,读取一个0到1000之间的整数并计算它各位数 字之和。例如:如果一个整数是932,那么它各位数字之和就是14。(提示:使用%来提取数字, 使用//运算符来去除掉被提取的数字。例如: 932%10=2 而932//10=93.)这里是一 个示例运行。

**
Enter a number between 0 and 1000: 999| J Er er
The sum of the digits is 27

程序编辑:

# 提示:使用%来提取数字,使用//运算符来去除掉被提取的数字
# 1.观察数字是一个0-1000之间的整数,则这个数必定是一个三位数的整数


number =  int(input("Enter a number between 0 and 1000:") )
_num1 = number % 10  #取余数    输入数字的倒数第一个数
_num2 = number // 10 #取百位和十位数
_num3 = _num2 % 10 # 取余数     输入数字的倒数第二个数
_num4 = _num2  // 10 #取百位和十位数
_num5 = _num4 % 10 # 取余数     输入数字的倒数第三个数
sum = _num1 + _num3 + _num5
print("The sum of the digits is %d"%(sum ) )

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day03.py
Enter a number between 0 and 1000:999
The sum of the digits is 27

Process finished with exit code 0

在这里插入图片描述

Demo04
**

(计算年数和天数)编写一个程序,提示用户输人分钟数(例如: 1 000 000).然后将分钟转换为 年数和天数并显示的程序。为了简单起见,假定一年有365天。这里是一个示例运行。

**
Enter the number of mi nutes: 1000000000
1000000000 mi nutes is approximately 1902 years and 214 days

程序编辑:

# 数据:分钟数
# 步骤:
# 1.输入一个分钟数
# 2.根据一年等于365*24*60分钟,一天等于24*60分钟
# 3.根据分钟数先计算多少年,余下的分钟数计算多少天

min = int(input("Enter the number of minutes:"))
_num1 = min // (365*24*60) #求年数
_num2 = min %  (365*24*60) #求余下的分钟数
_num3 = _num2 // (24*60) #求天数
print("%d minutes is approximately %d years and %d days"%(min,_num1 ,_num3 ))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day04.py
Enter the number of minutes:1000000000
1000000000 minutes is approximately 1902 years and 214 days

Process finished with exit code 0

在这里插入图片描述
Demo05
**

(科学:计算能量)编写–个程序,计算将水从初始温度加热到最终温度所需的能量。你的程序应 该提示用户输人以千克计算的水量以及水的初始温度和最终温度。计算能量的公式是 Q = M *(finalTemperature - initialTemperature) ★4184 这里的M是按千克计的水量,温度为摄氏温度,热量Q以焦耳计。这里是一个示例运行。

**
Enter the amount of water in kilograms: 55.5 Enter
Enter the initial temperature: 3.5一 t
Enter the final temperature: 10.5 enter
The energy needed is 1625484.0

程序编辑:

# 数据:水的总量千克,初始温度,最终温度
# 步骤:
# 1.输入水的总重量(以公斤为单位),水的初始温度,水的最终温度
# 2.列出公式,计算值
# 3,输出热量Q的值

M =  float (input("Enter the amount of water in kilograms:"))
initialTemperature =  float (input("Enter the initial Temperature:"))
finalTemperature =  float (input("Enter the final Temperature:"))
Q = M * (finalTemperature - initialTemperature) * 4184
print("The energy needed is %.1f\n"%(Q))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day05.py
Enter the amount of water in kilograms:55.5
Enter the initial Temperature:3.5
Enter the final Temperature:10.5
The energy needed is 1625484.0


Process finished with exit code 0

在这里插入图片描述

Demo06
**

(科学:风寒温度)室外有多冷?只有温度值是不足以提供答案的。其他因素,例如:风速、相对 湿度和光照都对室外寒冷程度有很大影响。在2001年,国家气象局( NWS)实行以新的利用温 度和风速来衡量风寒温度。这个公式如下所示。 te = 35.74+0.62151。- 35.75v.10 +0.42751。v210 这里的ta是华氏温度表示的室外温度,而v是以里1每小时计算的风速。1e 是风寒温度。该 公式不适用于风速在每小时2里以下或温度在-58华氏度以下及41华氏度以上。 编写一个程序,提示用户输人一个-58华氏度到41华氏度之间的温度和一-个大于等于每小 时2里的风速,然后显示风寒温度。这里是一个示例运行。

**
Enter the temperature in Fahrenheit between -58 and 41: 5.3 F a Bnter
Enter the wind speed in miles per hour: 6 e t
The wind chi11 index is -5. 56707
在这里插入图片描述
程序编辑:

# 数据:温度,风速
# 步骤:
# 1.输入一个温度在-58华氏度到41华氏度之间的温度,这里用t表示
# 2.输入一个大于等于每小时2里的风速,这里用v表示
# 3.列出风寒温度(Twc)计算公式
# 4.输出计算的值

t = float (input("Enter the temperature in Fahrenheit between -58 and 41: "))
v = float (input("Enter the wind speed in miles per hour: "))
Twc = 35.74 + 0.6215 * t - 35.75 * (v ** 0.16) + 0.4275 * (t * 0.16)
print("The wind chi11 index is %0.5f "%(Twc))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day06.py
Enter the temperature in Fahrenheit between -58 and 41: 5.3
Enter the wind speed in miles per hour: 6
The wind chi11 index is -8.22253 

Process finished with exit code 0

在这里插入图片描述

Demo07
**

(物理方面:计算跑道长度)假定给出飞机的加速度a和起飞速度v,可以根据以下公式计算出 飞机起”飞所需要的最短跑道长度。 编写一个程序,提示用户输人以米1秒(m/s)为单位的v和以米1秒的平方(m/s3)位单位 的a.然后显示最短的跑道长度。这里是一个示例运行。

Enter speed and acceleration: 60, 3.5 smer
The minimum runway 1ength for this airplane is 514.286 meters
在这里插入图片描述

程序编辑:

# 数据:加速度a,起飞速度v
# 步骤:
# 1.输入飞机的加速度和起飞速度
# 2.列出最短跑道长度计算公式
# 4.输出计算的值

v , a = eval((input("Enter speed and acceleration: ")))
length = (v ** 2) / (2 * a)
print("The minimum runway 1ength for this airplane is %0.3f meters  "%(length ))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day07.py
Enter speed and acceleration: 60,3.5
The minimum runway 1ength for this airplane is 514.286 meters  

Process finished with exit code 0

在这里插入图片描述

Demo08
(分割数字)编写一个程序,提示用户输人四位整数并以反向顺序显示。这里是一个示例运行。
Enter an integer: 5213 Enter
3
1
2
5

程序编辑:

#  提示:使用%来提取数字,使用//运算符来去除掉被提取的数字
# 1.观察数字是一个四位数的整数
# 步骤
# 1.输入这个四位数的整数
# 2.使用%来提取数字,使用//运算符来去除掉被提取的数字
# 3.把提取出来的数字,输出

number =  int(input("Enter an integer:") )
_num1 = number % 10  #取余数    输入数字的倒数第一个数
_num2 = number // 10 #取千位,百位和十位数
_num3 = _num2 % 10 # 取余数     输入数字的倒数第二个数
_num4 = _num2  // 10 #取千位和百位数
_num5 = _num4 % 10 # 取余数     输入数字的倒数第三个数
_num6 = _num4  // 10 #取千位
_num7 = _num6 % 10 # 取余数     输入数字的倒数第四个数
print("%d\n%d\n%d\n%d\n"%(_num1 ,_num3 ,_num5 ,_num7 ) )

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day01/day08.py
Enter an integer:5213
3
1
2
5


Process finished with exit code 0

在这里插入图片描述

在这里插入图片描述

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值