正在看书自学中,贴出自己的作业,欢迎大家交流指正,一起进步~
月球上物体的体重是在地球上的16.5%,假如你在地球上每年增长0.5kg,编写程序输出未来十年你在地球和月球上的体重。
# 3.1 weight
weight = 50
n = 0.5/weight
for i in range(10):
weight = weight*(1+n)
print("On earth: {:.2f}kg".format(weight))
print("On moon: {:.2f}kg".format(0.165*weight))
On earth: 55.23kg
On moon: 9.11kg
尽管每天坚持,但人的能力发展不是无限的,符合模型:7天为一个周期,连续学习3天能力值不变,第4天开始到第7天每天能力增长为前一天的1%,如果7天中有1天间断学习,则周期从头计算。请编写程序回答,如果初始能力值为1,连续学习365天后能力值为多少?
# 3.2
capacity = 1
n = 0.01
for i in range(365):
if i%7 not in [0,1,2]:
capacity = capacity*(1+n)
else:
capacity = capacity
print("Keep Learning 365 days, capacity is: {:.2f}." .format(capacity))
Keep Learning 365 days, capacity is: 7.92.
# 3.3
n = 0.01
off = [10,15]
for k in off:
capacity = 1
j = 0
for i in range(365):
if i%k != 0:
if (i-(k+1)*j)%7 not in [1,2,0]:
capacity = capacity*(1+n)
else:
capacity = capacity
else:
j += 1
capacity = capacity
print("Rest every {:d} days, capacity is: {:.2f} after 365 days." .format(k,capacity))
Rest every 10 days, capacity is: 6.62 after 365 days.
Rest every 15 days, capacity is: 7.03 after 365 days.
设n是一任意自然数,如果n的个位数字反向排列所得自然数与n相等,称n为回文数。从键盘输入一个5位数字,编写程序判断这个数字是不是回文数。
# 3.4
while 1:
string = input("Input 5 numbers: ")
if string != '':
reverse = string[-1::-1]
if string == reverse:
print("{} is symmetric.".format(eval(string)))
else:
print("{} is not symmetric.".format(eval(string)))
else:
break
Input 5 numbers: 12345
12345 is not symmetric.
Input 5 numbers: 12321
12321 is symmetric.
#3.5
for k in range(11):
if k in [0,5,10]:
for i in range(2):
print('+',end=' ')
for j in range(4):
print('--',end=' ')
print('+')
else:
for i in range(2):
print('|',end=' ')
for j in range(4):
print(' ',end=' ')
print('|\n')
+ -- -- -- -- + -- -- -- -- +
| | |
| | |
| | |
| | |
+ -- -- -- -- + -- -- -- -- +
| | |
| | |
| | |
| | |
+ -- -- -- -- + -- -- -- -- +
这道题我总是觉得不太对,一开始"+“中间用的是”-",发现是个长方形,于是去掉了每行"|“之间的换行,虽然出来是正方形的效果,但是行间距不对。其实将”|"改成"1"看起来效果最好,但依旧是很奇怪,希望有做出来和题目完全一致的朋友指教一下~
打印如下形式的进度条。
Starting…Done!
# 3.6
import time
for i in range(52): # i from 0 to 51
if i not in [51]:
time.sleep(0.05)
print('\rStarting {:.0f}%' .format(i*2),end='')
else:
print("\rStarting...Done!")