本文练习题答案仅涉及编程部分,其余部分略过。每一道题都经过运行确保正确,仅供参考!
Chapter 1 为什么要学编程?
较简单、概念化。略
Chapter 2 变量、表达式、语句
习题 2: 使⽤ input 编写⼀个程序,提⽰⽤⼾输⼊姓名,然后打印欢迎语。
name = input("Enter your name:\n")
print("Hello " + name)
习题 3: 编写⼀个程序,提⽰⽤⼾输⼊⼯时和时薪,然后计算出总⼯资。
Hours = input("Enter Hours:\n")
Rate = input("Enter Rate:\n")
Pay = float(Hours) * float(Rate)
print("Pay:\n", Pay)
练习 5: 写⼀个程序,提⽰⽤⼾输⼊摄⽒温度,然后将其转化成华⽒温度,并且把结果打印出来。
a = float(input('请输入摄氏温度:'))
b = a * 9 / 5 + 32
print("摄氏温度{}转换为华氏温度为:{}".format(a, b))
Chapter 3 条件执行
Exercise 1: Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.(练习1:重写您的薪资计算,以使雇员工作时间超过40小时以上的小时工资为1.5倍。)
#练习1
prompt1 = input('Enter Hours:')
Hours = int(prompt1)
prompt2 = input('Enter Rate:')
Rate = int(prompt2)
if 0 <= Hours <= 40:
Pay = Hours * Rate
print(Pay)
if Hours > 40:
Pay = 40 * Rate +(Hours-40)*(1.5 * Rate)
print(Pay)
Exercise 2: Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printi