本篇是系统学习python时做的题目,应该都是挺老的,分享出来,供参考
代码都是当时敲出来的,不能够保证正确率与最完美的解法。
chap1
1.现在许多显示器的屏幕宽度和高度的比例是16:9。讨论显示器的尺寸讲的是对角线长度,单位是英寸。现编写程序,输人显示器的尺寸,单位英寸,计算并输出显示器的宽度和高单位为厘米。提示:1英寸(in)=2.54厘米(cm)
from math import sqrt
x = int(input("请输入显示器的尺寸(即对角线长)(单位为英寸):")) #用户输入尺寸
y = sqrt((x*2.54)**2/(9**2+16**2)) #运用计算公式得到y
length = 16*y #长
height = 9*y #宽
print("显示器的长为:",length,"(厘米)") #输出
print("显示器的长为:",height,"(厘米)")
2.体型判断:请用户输入身高和体重,使用BMI公式(体重/身高2)计算用户的BMI指数,并输出用户的体型信息。提示:BMI正常值在20至25之间,低于20为偏瘦,超过25为超重,30以上则属肥胖。
height = float(input("请输入您的身高(米):"))
weight = float(input("请输入您的体重(千克):"))
BMI = weight/height**2
if BMI < 20:
print("您的体型偏瘦!")
elif BMI > 25 and BMI < 30:
print("您的体型为超重!")
elif BMI >30:
print("您的体型为肥胖!")
else :
print("您的体型正常!")
3.剪刀石头布游戏。模拟电脑和玩家进行比赛,根据游戏规则判定输赢。
import random
print("剪刀石头布游戏!")
x = int(input("请输入您想出的动作(1为剪刀,2为石头,3为布,-1结束):"))
y = random.randint(1,3)
while x != -1:
if x == y:
print("平局!")
elif x == 1 and y == 2:
print("您输了!")
elif x == 1 and y == 3:
print("您赢了!")
elif x == 2 and y == 1:
print("您赢了!")
elif x == 2 and y == 3:
print("您输了!")
elif x == 3 and y == 1:
print("您输了!")
elif x == 3 and y == 2:
print("您赢了!")
x = int(input("请输入您想出的动作(1为剪刀,2为石头,3为布,-1结束):"))
y = random.randint(1,3)
if x == -1:
print("游戏结束!")
chap2
1.一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。求它在第n次落地时,共经过多少米?
height = 100
total = 100
n = int(input("您想查看第n次落地时,共经过多少米:"))
i = 1
while n > i:
height = height/2
total += 2*height
i += 1
print("在第",n,"次落地时,共经过",total,"米")
2.登录测试。使用字典保存多个用户的用户名和密码,当用户输入用户名和密码时,测试是否匹配,匹配则打印“登录成功”,否则打印“账户或密码错误”,每个用户允许有3次重试机会。
account = {'admin':'admin' ,
'teacher':'teacher' ,
'student':'student' ,
'world':'world',
'china':'china',
'sun':'sun'} #储存的账号密码
time = 0 #记录次数
success = 0 #判断是否登录成功
while time < 3:
account0 = str(input('请输入账户名:'))
password0 = str(input('请输入密码:'))
time += 1
for i in account:
if i == account0:
if password0 == account[i]:
print("登录成功!")
success = 1 #登录成功标志
break
else:
print("账户或密码错误!")
if success == 0:
print('账户或密码错误,请再试一次!')
if time == 3 and success == 0:
print('您已连续输错三次,登陆失败!')
3.录入N个学生的成绩,把该学生的成绩转换成A:优秀、B:良好、C:合格、D:及格的形式,最后将该学生的成绩打印出来。要求使用try...except处理输入异常、assert断言处理分数不合理的情况。
n = 1
number = []
score = []
level = []
switch = 0
score0 = 0
while True:
try:
number.append(n)
score0 = int(input('请输入成绩:'))
assert score0 > 0, print('成绩必须为大于0的整数')
score.append(score0)
n += 1
if score0 >= 90:
level0 = 'A:优秀'
elif score0 >= 80 and score0 < 90 :
level0 = 'B:良好'
elif score0 >= 70 and score0 < 80 :
level0 = 'C:合格'
elif score0 >= 60 and score0 < 70:
level0 = 'D:及格'
else:
level0 = '不及格'
level.append(level0)
except :
print("输入错误")
switch = int(input('是否继续,继续请输入1,否则请输入-1:'))
if switch == -1:
break
i = 0
while i < n-1:
print(number[i],':',level[i])
i+=1
chap3
1.请编写一个函数SDSearch(txt, word),其中,txt是一段文本,word是给定的词汇,函数SDSearch可以找到word在txt中的所有位置,并将它们作为返回值返回,编写函数main()调用SDSearch(txt, word),输出SDSearch(txt, word)返回结果。(提示:find(字符串,查找的起始位置))
def SDSearch(txt,word):
pos = 0 #所找word的位置
position = [] #存储所有出现的位置
t = 0 #跳过已找到的位置
while pos != -1:
pos = txt.find(word,t,len(txt)) #find函数查找
t = pos+len(word) #跳过已找到的
position.append(pos) #记录
return position
if __name__ == '__main__': #主函数
txt = 'Murder on the Orient Express tells the tale of thirteen strangers stranded on a train, where everyones a suspect...'
word = 'strangers'
P = SDSearch(txt,word)
if P[0] != -1: #判断是否存在
for i in P:
if i != -1:
print(word,'为',txt,'的第',i+1,'个字') #输出
else:
print(word,'不存在于',word,'中')
2.编写函数SDrepCount(txt, word, repword),其中,txt是一段文本,word是给定的将要被替换词汇,repword是给定替换的词汇,SDrepCount(txt, word, repword)可以用repword替换txt中出现的所有word,并返回替换的次数。编写函数main()调用SDrepCount(txt, word, repword),输出SDrepCount(txt, word, repword)返回结果。
def SDrepCount(txt,word,repword):
t = 0 #记录次数
aftertxt = txt #存储替换后的txt
while aftertxt.find(word) != -1: #如果还存在需要替换的词汇则循环
aftertxt = aftertxt.replace(word,repword,1) #一次一次地替换
t += 1
return t,aftertxt #返回替换的次数和替换后的txt
if __name__ == '__main__':
txt = 'Murder on the Orient Express tells the tale of thirteen strangres stranded on a train, where everyones a suspect...'
word = 'strangres'
repword = 'strangers'
t,aftertxt = SDrepCount(txt,word,repword)
print("替换前:",txt)
print("替换完成,共替换",t,'次!')
print("替换后:",aftertxt)
3.韩信有一队兵,他想知道有多少人,便让士兵排队。他命令士兵3人一排,结果多出2名;接着命令士兵5人一排,结果多出3名;他又命令士兵7人一排,结果又多出2名。程序编写要求如下:
(1)请求出韩信至少拥有的士兵数目。
(2)建立函数完成此题,函数的传入参数为三种排法的余量,返回值为韩信至少拥有的士兵数目,如
def numberOfSoldier(a, b, c): //a为3人一排的余量,a<3
…… //b为5人一排的余量,a<5
//c为7人一排的余量,c<7
return sTotal
def numberOfSoldier(a,b,c):
for sTotal in range(1,1000): #穷举法
if sTotal % 3 == a and sTotal % 5 == b and sTotal % 7 == c:
break #满足条件则退出循环,即得最小值
else:
sTotal += 1
return sTotal
if __name__ == '__main__':
print('韩信至少拥有的士兵数目为:',numberOfSoldier(2,3,2))
这一部分应该问题没有那么大,后面更复杂的一部分在Python学习题目综合2,但还是最好自己试试,毕竟不能够保证正确率的。
在学习的过程中参考了很多分享的内容,对这些无私分享表示感谢!
相关链接:Python学习题目综合2
相关链接:Python题3(泰坦尼克数据分析)