此篇文章详细记录了python的简单案例,以及课后习题,对应的教材为 Python语言程序设计(清华大学出版社)
期末编程大题可能会从这里出,注意只是可能。
但以下都是常见的例子。
例题原文来自https://blog.ohmykreee.top/
答案作者为:Kreee & 张忆文(文忆天下)
部分我觉得不是很好的,或没有答案的问题,代码由Windsky Yan来写。
已经确保所有案例都通过 Pycharm 运行成功。当然,一个程序,不止一种形式的代码,但殊途同归。如果有更好的算法也欢迎提出。
祝满绩。
1.从键盘输入三个数值,分别赋值给num1, num2和num3,并求它们的平均值。
num1 = eval(input('请输入第一个数字:'))
num2 = eval(input('请输入第二个数字:'))
num3 = eval(input('请输入第三个数字:'))
averNum = (num1 + num2 + num3) / 3
print('这三个数的平均数为:{}'.format(averNum))
2. 输入一个三位数,输出它的逆序数,例如:输入123,输出321。
a = int(input('请输入一个三位整数:'))
a1 = a % 10
a2 = a // 10 % 10
a3 = a // 100 % 10
outNum = str(a1) + str(a2) + str(a3)
print('该三位数的逆序数为:{}'.format(outNum))
面代码通过[]切片实现
a = input('请输入一个三位整数:')
print('该三位数的逆序数为:', a[::-1])
3. 输入一个摄氏温度,输出对应华氏温度。
c = eval(input("Enter a degree in Celsius:"))
f = (9 / 5) * c + 32
print( "%d Celsius is %.1f Fahrenheit" %(c, f))
4. 使用键盘输入一个 Unicode 字符,显示出这个字符对应的 Unicode 编码值。
getInput = input('请输入一个字符:')
outOrd = ord(getInput)
print('字符 {} 的 Unicode 编码值为:{}'.format(getInput, outOrd))
5. 在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密、恺撒变换、变换加密,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。请使用键盘输入偏移量,并使用偏移量对键盘输入对单个大写英文字母进行加密。如偏移量为3,输入英文字母为Z,则输出为C。
a = int(input("输入的偏移量为:"))
b = input("输入单个大写英文字母为:")
c = (ord(b) - ord("A") + a) % 26
# 视A-Z为26个字母一个循环,取余数
d = chr(ord("A") + c)
print("经过凯撒加密之后,输出的字符为:" + d)
6. 使用 random.randint(a, b) 方法,随机生成三个100以内的自然数,求三个数的和。
# 注意的是randint是闭区间,random是前闭后开。
import random
num1 = random.randint(0, 100)
num2 = random.randint(0, 100)
num3 = random.randint(0, 100)
Num = num1 + num2 + num3
print('随机产生的三个数为 {}, {}, {}, 它们的和为{}'.format(num1, num2, num3, Num))
7.使用 round(x, y) 函数,可以将浮点数x保留y位小数。使用键盘,输入两个非零数,求这两个数的商,结果保留两位小数。输出的时候,请注意使用“➗”(十进制Unicode编码:10135)作为连接符表示除号。
num1 = eval(input("请输入被除数:"))
num2 = eval(input("请输入除数:"))
outNum = round(num1 / num2, 2)
# 第一次写的时候用\n一直转义不出来➗,后来才意识到用chr()函数。。。
print(str(num1) + chr(10135) + str(num2) + '=' + str(outNum))
8.使用 time.time() 方法能够得到目前的时间点,距离1970年1月1日0时0点0分0秒已经过去了多少秒。已知,1970年1月1日星期四,使用计算机计算得出:
(1)今天距离1970年1月1日,过了多少天
(2)今天是星期几。
# time.time函数:获取程序运行当时的时间值。这个时间值是一个浮点数,整数部分为秒数,这个秒数是从格林尼治时间1970年1月1日0点开始计算的。
import time
currTime = time.time()
dayPassed = int(currTime // (60 * 60 * 24))
weekPassed = int(dayPassed % 7)
# 1970年1月1日为周4,7-4=3
if weekPassed > 3:
currWeek = weekPassed - 3
else:
currWeek = weekPassed + 4
print('今天距离1970年1月1日,过了 {} 天,今天是星期{}'.format(dayPassed, currWeek))
9.解一元二次方程组,ax^2 + bx + c = 0,时,有三种可能情况,分别为:
1、 有两个不等实根
2、 有两个相等实根
3、 无实根。
请使用键盘输入a, b, c的值,并输出一元二次方程的解。
a, b, c = eval(input("输入一元二次方程的 a,b,c 的值以逗号隔开:"))
if a == 0:
if b == 0:
if c == 0:
print("该方程有任意解")
else:
print("该方程无解")
else:
print("该方程有唯一解且解为x1={}".format(-c/b))
else:
delta = b ** 2 - 4 * a * c
if delta < 0:
print("这个方程无实数解")
elif delta == 0:
root = (-b) / (2 * a)
print("这个方程有两个相等的根,其值为x1=x2={:.2f}".format(root))
else:
root1 = ((-b) + delta ** 0.5) / (2 * a)
root2 = ((-b) - delta ** 0.5) / (2 * a)
print("这个方程有两个不同的根: x1 = {:.2f},其值为x2={:.2f}".format(root1, root2))
10.空气污染指数api的取值与对应的空气质量关系如下:0~50为优, 51~99为良,100~199为轻度污染,200~299为中度污染,300以上为重污染。请编写程序,从键盘输入api值,并输出api值所对应的空气质量。
getApiNum = int(input('请输入空气污染指数:'))
if getApiNum <= 50:
outResult = '优'
elif 51 <= getApiNum <= 99:
outResult = '良'
elif 100 <= getApiNum <= 199:
outResult = '轻度污染'
elif 200 <= getApiNum <= 299:
outResult = '中度污染'
elif 300 <= getApiNum:
outResult = '重度污染'
print('空气质量为:{}'.format(outResult))
11.蔡勒(Zeller)公式,是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星期几。蔡勒(Zeller)公式为:
w = (y + [y / 4] + [c / 4] - 2c + [26(m + 1) / 10] + d - 1) % 7
其中:
w:代表星期几;w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
c:世纪数(注:一般情况下,在公式中取值为已经过的世纪数,也就是年份除以一百的结果,c应该等于所在世纪的编号,如公元2021年,c就等于20)
y:世纪的年数(一般情况下是年份的后两位数,如公元2021年,y就等于21)
m:月份(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d:日
[ ]代表取整,即只要整数部分。
请使用计算机编写程序,输入年、月、日,输出对应星期几。
year = int(input("输入年份:"))
month = int(input("输入月份:"))
day = int(input("输入日:"))
m = month
if month < 3:
m = month + 12
year = year - 1
c = year // 100
y = year % 100
d = day
w = (y + (y // 4) + (c // 4) - 2 * c + (26 * (m + 1) // 10) + d - 1) % 7
if(w == 1):
message = "星期一"
elif (w == 2):
message = "星期二"
elif (w == 3):
message = "星期三"
elif (w == 4):
message = "星期四"
elif (w == 5):
message = "星期五"
elif (w == 6):
message = "星期六"
else:
message = "星期日"
print("当日为:{}".format(message))
或者通过列表实现。
year = int(input("输入年份:"))
month = int(input("输入月份:"))
day = int(input("输入日:"))
m = month
if month < 3:
m = month + 12
year = year - 1
c = year // 100
y = year % 100
d = day
w = (y + (y // 4) + (c // 4) - 2 * c + (26 * (m + 1) // 10) + d - 1) % 7
w = int(w)
list = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
print("当日为:", list[w])
12.输入两个圆的圆心坐标及这两个圆对应的半径,求这两个圆之间的关系,是内含、内切,相交、外切还是分离?
x1, y1 = eval(input("输入一个圆的圆心坐标:"))
r1 = eval(input("输入圆的半径:"))
x2, y2 = eval(input("输入另一个圆的圆心坐标:"))
r2 = eval(input("输入圆的半径:"))
distance = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
if distance < abs(r1 - r2):
print("这两个圆的关系是:内含")
elif distance == abs(r1 - r2):
print("这两个圆的关系是:内切")
elif distance < r1 + r2:
print("这两个圆的关系是:相交")
elif distance == r1 + r2:
print("这两个圆的关系是:外切")
else:
print("这两个圆的关系是:分离")
13. 剪刀、石头布、是一种划拳游戏。剪刀赢布、布赢石头、石头赢剪刀。假设使用三个整数0、1、2来分别代表石头、剪刀、布,计算机随机生成三个整数(0、1、2)中的一个,用户使用键盘输入(0、1、2)中的一个整数,程序判断是计算机赢了,还是用户赢了,还是平局。
import random
list1 = ['剪刀。', '石头。', '布。']
a = eval(input('剪刀(0),石头(1),布(2):'))
b = random.randint(0, 2)
if a == b:
print('计算机是', list1[b], '你是', list1[a], '平局。')
elif a - b == 1 or a - b == -2:
print('计算机是', list1[b], '你是', list1[a], '你获胜了。')
else:
print('计算机是', list1[b], '你是', list1[a], '你输了。')
14.某公司进行绩效分配的时候,需要编制一个计算机程序帮助计算奖金。假设一个部门经理的全部门拿到了500万元以上的订单,则部门经理获得总订单额度的1%作为个人绩效奖励,如果没有达到这个额度,则只能获得订单额度的0.5%作为个人绩效奖励;一个普通员工如果能够拿到50万元以上的订单,则该员工获得订单额度的2%作为个人绩效奖励,如果没有达到这个目标,则该员工仅获得订单额度的1%作为个人绩效奖励。编制一个程序,使用键盘输入必要参数,计算出某公司员工的绩效。
position = input("输入职位,经理或普通员工:")
order = eval(input("输入订单总额(单位:万元):"))
if position == "经理":
if order > 500:
bonus = order * 0.01
else:
bonus = order * 0.005
else:
if order > 50:
bonus = order * 0.02
else:
bonus = order * 0.01
print("该员工的职位为{},绩效奖金为{}万元".format(position, bonus))
15.随机生成一个取值范围介于[0, 51]的整数,用以模拟从52张扑克牌中随机抽取一张扑克牌的操作。在本程序中,需要在用牌花色(黑桃、红心、梅花、方片)和牌面(“A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”)在控制台显示出随机抽到的牌。
import random
card = random.randint(0, 51)
message = "你抽到的牌是:"
# suit 代表花色,黑桃(0)、红桃(1)、梅花(2)、方片(3),输出花色
suit = card % 4
if suit == 0:
message = message + "黑桃"
elif suit == 1:
message = message + "红桃"
elif suit == 2:
message = message + "梅花"
else:
message = message + "方片"
# 接下来输出牌面
cardnumber = (card % 13 + 1)
if cardnumber == 1:
message = message + "A"
elif cardnumber == 11:
message = message + "J"
elif cardnumber == 12:
message = message + "Q"
elif cardnumber == 13:
message = message + "K"
else:
message = message + str(cardnumber)
print(message)
或者你可以这样写:
import random
getRandom = random.randint(0, 51)
pokerList = []
for suit in ['黑桃', '红桃', '梅花', '方片']:
for num in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
pokerList.append(suit + num)
print('你抽到的牌是:{}'.format(pokerList[getRandom]))
16.我国使用的是阶梯电价制度。按照用电的电量情况可以分为三个档次:
第一档,电量每户每月210度及以下
第二档,电量每户每月210-400度之间
第三档,电量每户每月400度以上。
一档、二档、三档就是根据不同用电量规定的电价,用电量大,电价就高,比如第一档电量每户每月执行电价,每度0.5469元,第二档电量每户每月在第一档电价基础上,每度加价0.05元,即每度0.5969元,第三档电量每户每月在第一档电价基础上,每度加价0.3元,即每度0.8469元。
如:某用户一个月用电800度,则计算公式可以表达为:
电费 = 第一档用电量(210度) 第一档电价+第二档用电量(400 - 210度) 第二档电价+第三档用电量(800 - 400度)* 第三档电价
使用键盘输入某用户的一个月用电量,求该用户需要缴纳多少电费。
getInput = eval(input('请输入这个月所用电度数:'))
if getInput <= 210:
outMoney = getInput * 0.5469
elif 210 < getInput <= 400:
outMoney = 210 * 0.5469 + (getInput - 210) * 0.5969
elif 400 < getInput:
outMoney = 210 * 0.5469 + (400 - 210) * 0.5969 + (getInput - 400) * 0.8469
print('这个月的电费为:{}'.format(outMoney))
补充:统计正数和负数个数然后计算这些数的平均数
编写一个程序来读入不指定个数的整数,然后决定已经读取的整数中有多少个正数和多少个负数并计算这些输入值的总和,最终获得他们的平均值。这个程序以0来结束,使用浮点数显示这个平均值。
# 逻辑关系不太会处理,while也不熟悉,总之我太菜了,将就着看吧
b = 0
c = 0
sum = 0
count = 0
a = eval(input('Enter an integer, the input ends if it is 0:'))
if a != 0 :
sum = sum + a
if a > 0:
b += 1
elif a < 0:
c += 1
while a != 0 :
a = eval(input('Enter an integer, the input ends if it is 0:'))
count += 1
if a > 0:
b += 1
elif a < 0:
c += 1
sum = sum + a
average = sum / count
print('The number of positives is', b)
print('The number of negatives is', c)
print('The total is', sum)
print('The average is', average)
else:
print('You didn\'t enter anything')
17.输出100以内所有7的倍数。
outNum = 7
i = 1
while outNum < 100:
print(outNum, end=' ')
i = i + 1
outNum = 7 * i
18.输出以下由“*”组成的图形:
*
**
***
****
*****
*
**
***
****
*****
*
***
*****
*******
*********
print('(1)')
for i in range(1, 6):
printLine = '*' * i
print(printLine)
print('(2)')
for j in range(1, 6):
printLine = ' ' * (5 - j) + '*' * j
print(printLine)
print('(3)')
for k in range(1, 6):
printLine = ' ' * (5 - k) + '*' * (2 * k - 1)
print(printLine)
19.使用键盘输入10个数,求这10个数的平均数。
number = 10
count, sum = 0, 0
list1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
while count < number:
num = eval(input("输入第" + list1[count] + "个数: "))
sum += num
count += 1
print("十个数的平均数是:" + str(sum / number))
20.(1)假设某门面出租,在租赁合同中规定的首年租金为100000块,租金每年涨5%。在控制台打印出10年时间,每一年租金会分别涨到多少钱?
(2) 如果某打工人想租下了该店面,花费了30万进行装修,开始通过销售服装获取收入。第一个月刨除房租、水电、人工开销,能有收入5000块。生意越来越好做,假设能够勉强维持每月收入7%递增,那需要多久的辛勤劳动,就能收回成本?
本题中,所有金额保留两位小数。
rental, year = 100000, 1
income, month, deposit = 10000, 1, 300000
while year <= 10:
print("第{}年的租金为¥{:.2f}".format(year, rental))
rental *= 1.05
year += 1
while deposit > 0:
deposit = deposit - income
month += 1
income *= 1.07
print("第{}月后能够收回投资".format(month))
或者可以这样实现:
# 计算门面租金
for i in range(0, 10):
rent = 100000.00 * pow(1.05, i)
print('第{}年的租金为:{:.2f}'.format(i + 1, rent))
# 计算劳动月数
getMonth = 0
addMoney = 0
while addMoney < 300000:
addMoney = addMoney + 10000 * pow(1.07, getMonth)
getMonth = getMonth + 1
print('第{}个月能够收回投资。'.format(getMonth + 1))
21.输出以下由数字组成的图形: 本题删除,请参考补充内容
补充:输出如图所示:
#个人觉得这样处理逻辑关系更加清晰
print('模式A')
for i in range(1, 7):
for j in range(1, i + 1):
if i >= j:
print(j, end=' ')
print()
print('模式B')
for i in range(6, 0, -1):
for j in range(1, i + 1):
if i >= j:
print(j, end=' ')
print()
print('模式C')
for i in range(1, 7):
message = " " * (6 - i)
print(message, end='')
for j in range(6, 0, -1):
if i >= j:
print(j, end=' ')
print()
print('模式D')
for i in range(6, 0, -1):
message = " " * (6 - i)
print(message, end='')
for j in range(1, i + 1):
if i >= j:
print(j, end=' ')
print()
补充:输出21世纪(从2001年到2100年)里所有的闰年,每行显示10个闰年,这些年份被一个空格隔开
count = 0
for year in range(2001, 2101):
if year % 4 == 0:
print(year, end=' ')
count += 1
# 执行换行操作
if count % 10 == 0:
print()
22.使用键盘输入一个年份,并在控制台输出这一年,每个月1日是星期几。
def zeller(year, month, date):
if month <= 2:
month = month + 12
year = year - 1
week = (date + 26 * (month + 1) // 10 + year % 100 + year % 100 // 4 \
+ year // 100 // 4 + year // 100 * 5 - 1) % 7
list = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
weekday = list[week]
return weekday
getYear = int(input('请输入一个年份:'))
for i in range(1, 13):
outWeek = zeller(getYear, i, 1)
print('{} 年的 {} 月 1 日是{}'.format(getYear, i, outWeek))
23.写一个程序,键盘输入一个十进制数,将十进制数转换为二进制数,输出到控制台。
getInput = eval(input('请输入需要转换为二进制的整数:'))
outBinary = ''
if getInput == 0:
outBinary = '0'
else:
divResult = getInput
while divResult != 1:
if divResult % 2 == 0:
outBinary = '0' + outBinary
divResult = divResult / 2
continue
else:
outBinary = '1' + outBinary
divResult = divResult // 2
continue
outBinary = '1' + outBinary
print(outBinary)
24.如果一个正整数等于除了他本身之外的所有正因子的和,那么这个数被称为是完全数。如:6 = 3 2 1 = 3 + 2 + 1,因此6是一个完全数。求10000以内所有的完全数。
for i in range(1, 10000):
sum = 0
for j in range(1, i // 2 + 1):
if i % j == 0:
sum = sum + j
if sum == i:
print(i, end=" ")
或者你可以这样实现,考虑到运行效率,更建议用第一种逻辑关系。
perfectNum = []
getInput = 10000
for num in range(1, getInput + 1):
divisor = []
for i in range(1, num):
if (num / i) % 1 == 0:
divisor.append(i)
addDivisor = 0
for j in divisor:
addDivisor = addDivisor + j
if addDivisor == num:
perfectNum.append(num)
for k in perfectNum:
print(k, end=" ")
25.定义一个名为 isLeapYear(year) 的函数,参数为一个年份,如果该年份是闰年,则返回值为True,否则,返回值为False。在同一源文件中,使用键盘输入年份,验证该函数是否能够正确返回该年份是否为闰年。
def isLeapYear(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
year = int(input("输入一个年份:"))
print(isLeapYear(year))
26.定义一个名为 zeller(year, month, date) 的函数,参数为年、月、日。通过这个函数计算并返回该日期是星期几。在同一源程序中,使用键盘输入年、月、日,验证该函数是否能正确计算出输入的日期为星期几。
def zeller(year, month, date):
if month <= 2:
month += 12
year -= 1
weekDay = (date + 26 * (month + 1) // 10 + year % 100 \
+ year % 100 // 4 + year // 100 // 4 + year // 100 * 5 - 1) % 7
return weekDay
year, month, date = eval(input("请输入年,月,日:"))
weekDay = zeller(year, month, date)
print(weekDay)
27.定义一个名为 isPrime(number) 的函数,参数为一个正整数。通过使用这个函数,能够判断一个正整数,是否为素数,是素数则返回True,不是素数则返回False。在同一源程序中,使用键盘输入一个正整数,验证该函数是否能够正确判断输入数为素数。
def isPrime(n):
if a <= 0:
return '请输入一个正整数'
else:
for i in range(2, a // 2 + 1):
if a % i == 0:
result = False
break
elif a % i != 0:
result = True
return result
a = int(input('请输入一个正整数:'))
print(isPrime(a))
def isPrime(number):
result = True
for factor in range(2, number // 2 + 1):
if number % factor == 0:
result = False
return result
getInput = int(input('请输入一个正整数:'))
print(isPrime(getInput))
28.定义一个名为 primeNumbers(number) 的函数,参数为一个正整数。通过使用这个函数,能够输出小于number的所有素数,输出的时候,每行10个素数。在同一源文件中,使用键盘输入一个正整数,验证该函数的输出结果。
def primeNumbers(a):
count = 0
# 设置从2到输入数字的循环
for num in range(2, a):
# 先默认判断其为素数
b = True
# 开始验证是否为素数
for i in range(2, num // 2 + 1):
# 经历循环,若能被其他数整除,则判断为False赋值给b
if num % i == 0:
b = False
# 当num为素数时候:
if b:
count += 1
print(num, end='\t')
# 执行换行
if count % 10 == 0:
print()
a = int(input('请输入一个正整数:'))
primeNumbers(a)
写死我了aaaaa....
还有一些练习题没有记录,原因很简单,太多了。。。
I know this iff off topicc but I'm looking into starting
my owwn blog aand was wndering what all iis neeeded to get setup?
I'm assuming havinng a blopg likee yours would cost a pretty penny?
I'm noot very internet savvy so I'm not 100% sure. Any sjggestions
or advice would bbe greeatly appreciated. Appreeciate it
Obviously,maintain a blog cost little, but it needs a lot time.
太好了,正好想学Python
希望对你有点用hhhh
啊~~giegie好帅,我好爱