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 计算行驶距离
,提示用户输入汽车行驶的加速度和时间,然后计算并显示行驶的距离。(汽车从静止开始)
a = float(input("Enter acceleration: "))
t = float(input("Enter time traveled: "))
S = 0.5 * a * t ** 2
print("Your car traveled ",S," meters")
练习-5 温度转换
将华氏温度转换为与其相对应的开尔文温度。
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)