本书采用[美]Tony Gaddis编写,由苏小红等人翻译的《Python程序设计基础》(原书第5版)。记录下本书每章课后的编程题
本章介绍程序的开发周期、变量和数据类型和使用顺序结构编写的简单程序。学生可以学习如何编写简单的程序:从键盘读取输入、执行数学运算并输出到屏幕上。本章还对伪码和流程图等设计程序的工具进行了介绍。本章最后介绍机器龟图形库
#1个人信息
print("Name:"+"Tom")
print("Address:"+"QiLi Street "+"City:"+"NanJing "+"Province:"+"JiangSu "+"Postal Code:"+"12345")
print("Mobile Phone:"+"1234567890")
print("Major:"+"Computer Science and Technology")
#2销售预测
n=float(input("Estimated total scales:"))
print("The probable profit:",(n*0.23))
#3土地面积计算
n=float(input("A area of a piece of land in feet:"))
print("Mu of land:",n/43560)
#4购物总额
consumption_tax_rate=0.07
total_consumption=float(input("The price of first goods:"))
consumption_tax=total_consumption*consumption_tax_rate
accounts_payable=consumption_tax+total_consumption
print("Total Consumption:",total_consumption,"Consumption tax:",consumption_tax,"Accounts payable:",accounts_payable)
total_consumption=float(input("The price of second goods:"))
consumption_tax=total_consumption*consumption_tax_rate
accounts_payable=consumption_tax+total_consumption
print("Total Consumption:",total_consumption,"Consumption tax:",consumption_tax,"Accounts payable:",accounts_payable)
total_consumption=float(input("The price of third goods:"))
consumption_tax=total_consumption*consumption_tax_rate
accounts_payable=consumption_tax+total_consumption
print("Total Consumption:",total_consumption,"Consumption tax:",consumption_tax,"Accounts payable:",accounts_payable)
total_consumption=float(input("The price of fourth goods:"))
consumption_tax=total_consumption*consumption_tax_rate
accounts_payable=consumption_tax+total_consumption
print("Total Consumption:",total_consumption,"Consumption tax:",consumption_tax,"Accounts payable:",accounts_payable)
total_consumption=float(input("The price of fifth goods:"))
consumption_tax=total_consumption*consumption_tax_rate
accounts_payable=consumption_tax+total_consumption
print("Total Consumption:",total_consumption,"Consumption tax:",consumption_tax,"Accounts payable:",accounts_payable)
#5行驶里程
print("Mileage in 6 hours(feet):",70*6)
print("Mileage in 10 hours(feet):",70*10)
print("Mileage in 15 hours(feet):",70*15)
#6消费税
purchase_amount=float(input("Please entry purchase amount:"))
state_consumption_tax_rate=0.05
national_consumption_tax_rate=0.025
total_consuption_tax=purchase_amount*state_consumption_tax_rate+purchase_amount*national_consumption_tax_rate
total_consuption_amount=purchase_amount+total_consuption_tax
print("Purchase amount:",purchase_amount)
print("State consuption tax:",purchase_amount*state_consumption_tax_rate)
print("National consuption tax:",purchase_amount*national_consumption_tax_rate)
print("Total consuption tax:",purchase_amount*(national_consumption_tax_rate+state_consumption_tax_rate))
print("Total consuption amount:",total_consuption_amount+total_consuption_tax)
#7每加仑汽车的行驶里程
Miles_per_Gallon=float(input("Please entry Miles driven in feet:"))
Gallons_of_gase_used=float(input("Please entry gallons of gas used:"))
print("The MPG:",Miles_per_Gallon/Gallons_of_gase_used)
#8小费、税和消费总额
total_consumption=float(input("Please entry total consumption:"))
tip_tax_rate=0.18
consumption_tax_rate=0.07
print("Tip:",tip_tax_rate*total_consumption)
print("Consumption tax:",total_consumption*consumption_tax_rate)
print("Total consumption amount:",total_consumption*(1+tip_tax_rate+consumption_tax_rate))
#9摄氏温度与华氏温度的转换程序
C=float(input("Please enter a Celsius temperature:"))
print("The corresponding Fahrenheit temperature is",9*C/5+32)
#10配料调节器
n=int(input("Please enter the number of dessert:"))
m=n/48
print("The number of sugar:",m*1.5)
print("The number of cream:",m)
print("The number of flour:",m*2.75)
#11性别比例
n=int(input("Please enter the number of male:"))
m=int(input("Please enter the number of female:"))
print("The male ratio:",n/(m+n))
print("The female ratio:",m/(n+m))
#12股票交易程序
payment_first=2000*40
print("The payment of Joe purchased stock:",payment_first)
print("The commission amount:",payment_first*0.03)
collection_second=2000*42.75
print("The commission amount:",collection_second*0.03)
print("The remains:",collection_second*(1-0.03)-payment_first*0.03)
#13葡萄种植
R=float(input("Please enter the length of each row of land in feet:"))
E=float(input("Please enter the width occupied by end post groups in feet:"))
S=float(input("Please enter spacing between vines in feet:"))
V=(R-2*E)/S
print("The number of the tree to plant:",V)
#14复利
P=float(input("Please enter principal amount initially deposited in the account:"))
r=float(input("Please enter annual interest rate:"))
n=int(input("Please enter the number times to calculate compound interest:"))
t=int(input("Please enter deposit year:"))
A=P*((1+r/n)**(n*t))
print("The total amount:",A)
#15机器龟绘图 Part1
import turtle
turtle.hideturtle()
turtle.fillcolor('blue')
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.end_fill()
#15机器龟绘图 Part2
import turtle
turtle.hideturtle()
turtle.left(60)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.fillcolor("blue")
turtle.begin_fill()
turtle.right(150)
turtle.forward(100/(3**0.5))
turtle.left(60)
turtle.forward(100/(3**0.5))
turtle.left(150)
turtle.forward(100)
turtle.end_fill()
#15机器龟绘图 Part3
import turtle
turtle.hideturtle()
turtle.forward(100)
turtle.right(45)
turtle.forward(100*(2**0.5))
turtle.right(45)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(45)
turtle.forward(100*(2**0.5))
turtle.right(45)
turtle.forward(100)
turtle.right(135)
turtle.forward(200*(2**0.5))
turtle.right(135)
turtle.forward(100)
turtle.right(90)
turtle.forward(200)
turtle.right(135)
turtle.forward(100*(2**0.5))
turtle.right(135)
turtle.forward(200)
turtle.done()
#15机器龟绘图 Part4
import turtle
turtle.speed(9)
turtle.hideturtle()
turtle.circle(50)
turtle.penup()
turtle.forward(120)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.forward(120)
turtle.pendown()
turtle.circle(50)
turtle.left(90)
turtle.penup()
turtle.forward(30)
turtle.left(90)
turtle.forward(60)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.forward(120)
turtle.pendown()
turtle.circle(50)
turtle.done()
#15机器龟绘图 Part5
import turtle
turtle.speed(9)
turtle.hideturtle()
turtle.forward(100)
turtle.write("East")
turtle.right(180)
turtle.forward(200)
turtle.write("West")
turtle.right(180)
turtle.forward(100)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.circle(10)
turtle.right(90)
turtle.forward(90)
turtle.write("North")
turtle.right(180)
turtle.forward(200)
turtle.write("South")
turtle.done()
#15机器龟绘图 Part6
import turtle
turtle.speed(9)
turtle.hideturtle()
turtle.dot()
turtle.forward(10)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(10)
turtle.dot()
turtle.right(90)
turtle.forward(90)
turtle.dot()
turtle.right(135)
turtle.forward(90/(2**0.5))
turtle.dot()
turtle.forward(90/(2**0.5))
turtle.left(135)
turtle.forward(90)
turtle.left(90)
turtle.dot()
turtle.forward(10)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.forward(10)
turtle.left(90)
turtle.forward(90)
turtle.left(135)
turtle.forward(90*(2**0.5))
turtle.done()