首页 - 通讯 -  python嵩天书3.12—3.15(思考与练习)、三章、四章

python嵩天书3.12—3.15(思考与练习)、三章、四章

2023-09-26 17:43

3.12一年365天,初始水平值为1.0,每天工作平均增加N,不工作水平不下降,一周工作4天,填写运算结果:

N=0
while N<=0.01:N=N+0.001dayup=1.0for i in range(365):if i % 7 in (5, 6, 0):# print("i")dayup = dayupelse:dayup = dayup * (1 + N)#print("{:.2f}".format(dayup))print("{:.2f}".format(dayup))

结果:
 

1.23   1.52   1.86   2.29   2.82   3.47   4.27   5.25   6.45    7.92

3.13   一年365天,初始水平值为1.0,每天工作平均增加N,不工作水平不下降,一周工作5天,填写运算结果:

N=0
while N<=0.01:N=N+0.001dayup=1.0for i in range(365):if i % 7 in ( 6, 0):     #同上相比,变化了5# print("i")dayup = dayupelse:dayup = dayup * (1 + N)#print("{:.2f}".format(dayup))print("{:.2f}".format(dayup))

结果:

1.30   1.68   2.18   2.82    3.66    4.74    6.13   7.94   10.27   13.29

3.14    一年365天,初始水平值为1.0,每天工作平均增加N,不工作水平不下降,一周工作6天,填写运算结果:

N=0
while N<=0.01:N=N+0.001dayup=1.0for i in range(365):if i % 7==0:                     #相比以上变了6# print("i")dayup = dayupelse:dayup = dayup * (1 + N)#print("{:.2f}".format(dayup))print("{:.2f}".format(dayup))

结果:

1.37   1.87   2.55    3.47   4.74   6.47   8.81   12.01   16.37   22.30

3.15  后续补充

程序练习题:

3.1  重量计算,重量计算。月球上物体的体重是在地球是的16.5,假如你在地球是每年增长0.5kg,编写程序输出未来10年你在地球和月球上的体重状况。

weight=eval(input("请输入体重:"))
earthweight=(weight+0.5*10)
moonweight=earthweight*0.165
print("在地球十年后体重:{:.2f}".format(earthweight))
print("再月球十年后体重:{:.2f}".format(moonweight))请输入体重:50
在地球十年后体重:55.00
再月球十年后体重:9.08

3.2   

3.3

3.4  判断回文

s=input("请输入5位数字:")
a=len(s)
i=0
while i<=(a/2):if s[i]==s[-(i+1)]:count=1             #这里使用count来判断回文i+=1else:count=0break
if count==1:print("该数字为回文")
else:print("该数字不是回文")

 

3.5  田字格的输出:

#i=1      
for i in range(1,12):#i=1if i %5==1:print("{0} - - - - {0} - - - - {0}".format("+", "+", "+"))else:#print("\n")print("{0:10s}{0:10s}{0}".format("|","|","|"))

 

第四章

4.1猜数游戏

m=eval(input("请输入一个整数:"))
p=5
count=1
while m!=p:count+=1if mp:print("太大了")#continuem = eval(input("请输入一个整数:"))#else:#count+=1
print("预测{}次,你猜对了".format(count))

 

4.2   统计不同字符个数

m=input("请输入不同字符:")       
countint=countstr=countspace=countother=0
for i in m:if i.isnumeric():     #判断是否为数字,注意这里是i.<方法名>countint+=1#print("")elif i.isalpha():    #判断是否为中英文字符countstr+=1elif i.isspace():                  #判断空格countspace+=1else:                #判断为其他countother+=1
print("数字个数为{},字符个数为{},空格个数为{},其他字符个数{}".format(countint,countstr,countspace,countother))

 

4.3  

4.4  猜数游戏,改编4.1,让计算机产生一个随机预设数字,范围在0~100,其他游戏规则不变。

from random import  *
m=eval(input("请输入一个整数:"))
p=randint(0,100)      #较4.1新加
count=1
while m!=p:count+=1if mp:print("太大了")#continuem = eval(input("请输入一个整数:"))#else:#count+=1
print("预测{}次,你猜对了".format(count))

4.5  猜数游戏续。当用户输入不是整数(如字母,浮点数时),程序会终止执行退出。当用户输入错误时,提示“输入内容必须是整数!”,并让用户重新输入。

from random import  *
def err():              #该程序还不健壮,无法在连续输入错误情况下依然能运行try:                #需要继续改写m=eval(int(input("请输入一个整数:")))return mexcept NameError:print("输入内容必须为整数!")#m = eval(input("请输入一个整数:"))m = eval(int(input("请输入一个整数:")))return mexcept:print("其他错误,输入内容必须为整数!")#m = eval(input("请输入一个整数:"))m = eval(int(input("请输入一个整数:")))return m
u=err()
p=randint(0,100)
print(p)
count=1
#try:
while u!=p:count+=1if up:print("太大了")#continue#m = eval(input("请输入一个整数:"))u=err()#else:#count+=1
print("预测{}次,你猜对了".format(count))

4.6 羊车门问题: