提高篇
1.控制台输入年龄,根据年龄输出不同的提示(例如:老年人、青壮年、成年人、
age = int(input('输入年龄:'))
if age < 12:
print('是儿童')
elif age < 18 : print('是未成年')
elif age < 30 : print('是成年人')
elif age < 60 : print('是青壮年')
else: print('是老人')
2.计算5的阶乘 5!的结果是
num = 5
num_jk = 1
for n in range(1, num + 1)
num_jk *= n
print(num, '的阶乘是:', num_jk)
3.求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘。
num = 20
sum_all = 0
product = 1
for n in range(1,num + 1):
product *= n
sum_all += product
print(product)
print(sum_all)
4.计算 1+1/2!+1/3!+1/4!+...1/20!=?
num = 20
sum_all = 0
rproduct = 1
for n in range(1,num + 1):
rproduct *= 1/n
sum_all += rproduct
print(rproduct)
print(sum_all)
5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果。
sum_all = 0
while 2 > 1:
num = int(input('宝贝输个数来看看:'))
if num == 0:
break
sum_all += num
print(sum_all)
6.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。
num1 = sum_all = a = 3
num = int(input('输入相加个数:'))
if num > 1:
for _ in range(num - 1):
num1 = num1 * 10 + a
sum_all += num1
print(sum_all)
else:
print(sum_all)
7.输入三个整数x,y,z,请把这三个数由小到大输出。
x = int(input())
y = int(input())
z = int(input())
for _ in range(3):
if x > y:
max = x
if max < z:
max = z
else:
max = y
if max < z:
max = z
print(max)
8.控制台输出三角形
num = int(input())
for n in range(num):
print('*' * (num - n))
b.
num = int(input())
for n in range(1,int(num / 2 +2 )):
print(' ' * int(num / 2 - n + 1),'*' * (2 * n - 1))
9.输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
for i in range(10):
for j in range(1,i + 1):
print(j,'x',i,'=' ,j * i,' ',end = '')
print()
效果:
10.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
for big in range(33):
for mid in range(50):
for sm in range(200):
if int(big * 3 + mid * 2 + sm / 2) == 100 and big + mid + sm ==100:
print('big = %d,mid = %d,sm = %d' % (big,mid,sm))
![图片.png](https://upload-images.jianshu.io/upload_images/14827757-d5b3440af0858ed2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
11.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的: 5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
for big in range(20):
for mid in range(33):
for sm in range(300):
if int(big * 5 + mid * 3 + sm / 3) == 100 and big + mid + sm ==100:
print('big = %d,mid = %d,sm = %d' % (big,mid,sm))
12.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元整好花掉,可如有哪些购买结合?
for big in range(7):
for mid in range(21):
for sm in range(51):
if int(big * 15 + mid * 5 + sm * 2) == 100 :
print('big = %d,mid = %d,sm = %d' % (big,mid,sm))
可有如下选择