Python入门之顺序控制结构练习

1.什么是顺序控制结构

        顺序控制结构是按语句在程序中出现的顺序逐行执行。

练习-1 计算平行四边形面积

base = float(input("Enter the length of Base: "))
height = float(inout("Enter the length of Height: "))

area = base * height

print("The area of the parallelogram is",area)

练习-2 计算圆的面积

radius = float(input("Enter the length of Radius: "))

area = 3.14159 * radius ** 2

print("The area of the circle is ",area)

练习-3 计算燃油经济性

在美国,汽车燃油经济性以每加仑跑的英里数进行衡量,或称为MPG。编写一个python程序,提示用户输入驾驶的总里程数和使用的汽油加仑数,然后使用程序计算并显示汽车的MPG。

miles_driven = float(input("Enter miles driven: "))
gallons = float(input("Enter gallons of gas used: "))

mpg = miles_driven / gallons

print("Your car's MPG is: ",mpg)

练习-4 计算行驶距离

S=v_0 + \frac{1}{2} at^2,提示用户输入汽车行驶的加速度和时间,然后计算并显示行驶的距离。(汽车从静止开始)

a = float(input("Enter acceleration: "))
t = float(input("Enter time traveled: "))

S = 0.5 * a * t ** 2

print("Your car traveled ",S," meters")

练习-5 温度转换

将华氏温度转换为与其相对应的开尔文温度。1.8 \times \text{Kelvin temperature} = \text{Fahrenheit temperature} + 459.67

Fahrenheit = float(input("Enter a temperature in Fahrenheit:"))
kevin = ( Fahrenheit + 459.67 ) / 1.8

print("The temperature in Kevin is ",kevin)

练习-6 计算消费税

销售员需要一个程序,用于输入产品的税前价格并计算其含税价格。假设增值税(VAT)为19%。

VAT = 0.19
price_before_tax = float(input("Enter the before-tax price of a product: "))

sales_tax = price_before_tax * VAT
price_after_tax = price_before_tax + sales_tax

print("The after-tax price is: ",price_after_tax)

练习-7 计算消费税和折扣

编写一个python程序,提示用户输入商品的税前价格和按百分比(0~100)提供的折扣。然后程序计算并显示新的价格。假设销售税为19%。

VAT = 0.19
price_before_discount = float(input("Enter the before-discount price of a product: "))
discount = int(input("Enter the discount offered (0-100): "))

discount_amount = price_before_discount * discount / 100
price_after_discount = price_before_discount - discount_amount

sales_tax = price_after_discount * VAT
price_after_tax = price_after_discount + sales_tax 

print("The discounted after-tex price is: ",price_after_tax)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值