1.控制台输入年龄,根据年龄输出不同的提示(例如:老年人,青壮年,成年人,未成年,儿童)
age = int(input("please input you age:"))
if age >= 60:
print("you are an Aged.")
elif age >= 25:
print('you are a young and young people.')
elif age >= 18:
print('you are an adult.')
elif age >= 12:
print('you are an under age.')
else:
print('you are a children')
'''
结果:
'''
2.计算5的阶乘 5!的结果是
sum = 1
for i in range(1,6):
sum *= i
print(sum)
'''
结果:
120
'''
3.求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘。
sum1 = 0 #记录总数
for i in range(1,21):
sum2 = 1 #记录每次阶乘的数
for j in range(1,i+1):
sum2 *= j
sum1 += sum2
print(sum1)
'''
结果:
2561327494111820313
'''
4.计算 1+1/2!+1/3!+1/4!+...1/20!=?
sum1 = 0 #记录总数
for i in range(1,21):
sum2 = 1
for j in range(1,i+1):
sum2 *= j
else:
sum2 = 1/sum2
sum1 += sum2
print(sum1)
'''
结果:
1.7182818284590455
'''
5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果。
sum = 0
while True:
num = int(input('please input a number(0 is over):'))
if num > 0:
sum += num
elif num==0:
break
print(sum)
'''
结果:
please input a number(0 is over):1
please input a number(0 is over):2
please input a number(0 is over):3
please input a number(0 is over):4
please input a number(0 is over):5
please input a number(0 is over):0
15
'''
6.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值
。
sum = 0
s = input("Please input numbers and add them together and add a few digits, separated by spaces.")
num = int(s.split(' ')[0])
length = int(s.split(' ')[1])
for i in range(1,length+1):
sum += int(str(num)*i)
print(sum)
'''
结果:
Please input numbers and add them together and add a few digits, separated by spaces.2 5
24690
'''
7.输入三个整数x,y,z,请把这三个数由小到大输出。
//方法一
num = []
for i in range(3):
n = input('please input a number:')
num.append(n)
num.sort()
for i in num:
print(i,end='')
print(" ",end='')
print()
#方法二
num = []
for i in range(3):
n = input('please input a number:')
num.append(n)
i = 0
while i < len(num)
m = num[i]
j = i+1
while j < len(num):
if m <= num[j]:
m = num[j]
num.insert(i,num.pop(j))
j -= 1
j += 1
i += 1
print(num)
#方法三
num = []
for i in range(3):
n = input('please input a number:')
num.append(n)
for i in range(0,len(num)):
for j in range(i+1,len(num)):
if num[i] <= num[j]:
t = num[i]
num[i] = num[j]
num[j] = t
print(num)
#方法四:最笨的方法
x = input('please input a number:')
y = input('please input a number:')
x = input('please input a number:')
if x>y and x>z and y>z:
print('%d %d %d'%(x,y,z))
elif x>y and x>z and z>y:
print('%d %d %d'%(x,z,y))
elif y>x and y>z and x>z:
print('%d %d %d'%(y,x,z))
elif y>x and y>z and z>x:
print('%d %d %d'%(y,z,x))
elif z>x and z>y and x>y:
print('%d %d %d'%(z,x,y))
else:
print('%d %d %d'%(z,y,x))
'''
结果:
please input a number:9
please input a number:4
please input a number:2
2 4 9
'''
8.控制台输出三角形
a.根据n的值的不同,输出相应的形状
n = 5时 n = 4
***** ****
**** ***
*** **
** *
*
b.根据n的值的不同,输出相应的形状(n为奇数)
n = 5 n = 7
* *
*** ***
***** *****
*******
#a.根据n的值的不同,输出响应的形状
num = int(input('please input a number:'))
while num > 0:
print('*'*num)
num -= 1
'''
结果:
please input a number:5
*****
****
***
**
*
'''
#根据n的值的不同,输出相应的形状(n为奇数)
n = int(input('please input an odd number:'))
i = 1
mid_n = n//2
while i <= n:
print(" "*mid_n,end='')
print('*'*i)
i += 2
mid_n -= 1
'''
结果:
please input an odd number:9
*
***
*****
*******
*********
'''
9.输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(i,j,i*j),end='')
print()
'''
结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
'''
10.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
big_horse = 0
midd_horse = 0
small_horse = 0
for i in range(0,100//3+1):
big_horse = i
for j in range(0,100//2+1):
midd_horse = j
for k in range(0,101,2):
small_horse = k
if big_horse*3+midd_horse*2+small_horse/2==100 and big_horse+midd_horse+small_horse==100:
print('big_horse:%d\nmidd_horse:%d\nsmall_horse:%d\n'%(
big_horse,midd_horse,small_horse
))
break
'''
结果:
big_horse:2
midd_horse:30
small_horse:68
big_horse:5
midd_horse:25
small_horse:70
big_horse:8
midd_horse:20
small_horse:72
big_horse:11
midd_horse:15
small_horse:74
big_horse:14
midd_horse:10
small_horse:76
big_horse:17
midd_horse:5
small_horse:78
big_horse:20
midd_horse:0
small_horse:80
'''
11.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的: 5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
cock = 0
hen = 0
chick = 0
for i in range(0,100//5+1):
cock = i
for j in range(0,100//3+1):
hen = j
for k in range(0,101,3):
chick = k
if cock*5+hen*3+chick/3==100 and cock+hen+chick==100:
print('cock:%d\nhen:%d\nchick:%d\n'%(cock,hen,chick))
break
'''
结果:
cock:0
hen:25
chick:75
cock:4
hen:18
chick:78
cock:8
hen:11
chick:81
cock:12
hen:4
chick:84
'''
12.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元整好花掉,可以有哪些购买结合?
shampoo = 0
soap = 0
toothbrush = 0
for i in range(0,100//15+1):
shampoo = i
for j in range(0,100//2+1):
soap = j
for k in range(0,100//5+1):
toothbrush = k
if shampoo*15+soap*2+toothbrush*5==100:
print('shampoo:%d\nsoap:%d\ntoothbrush:%d\n'%(shampoo,soap,toothbrush))
'''
结果:
shampoo:0
soap:0
toothbrush:20
shampoo:0
soap:5
toothbrush:18
shampoo:0
soap:10
toothbrush:16
shampoo:0
soap:15
toothbrush:14
shampoo:0
soap:20
toothbrush:12
shampoo:0
soap:25
toothbrush:10
shampoo:0
soap:30
toothbrush:8
shampoo:0
soap:35
toothbrush:6
shampoo:0
soap:40
toothbrush:4
shampoo:0
soap:45
toothbrush:2
shampoo:0
soap:50
toothbrush:0
shampoo:1
soap:0
toothbrush:17
shampoo:1
soap:5
toothbrush:15
shampoo:1
soap:10
toothbrush:13
shampoo:1
soap:15
toothbrush:11
shampoo:1
soap:20
toothbrush:9
shampoo:1
soap:25
toothbrush:7
shampoo:1
soap:30
toothbrush:5
shampoo:1
soap:35
toothbrush:3
shampoo:1
soap:40
toothbrush:1
shampoo:2
soap:0
toothbrush:14
shampoo:2
soap:5
toothbrush:12
shampoo:2
soap:10
toothbrush:10
shampoo:2
soap:15
toothbrush:8
shampoo:2
soap:20
toothbrush:6
shampoo:2
soap:25
toothbrush:4
shampoo:2
soap:30
toothbrush:2
shampoo:2
soap:35
toothbrush:0
shampoo:3
soap:0
toothbrush:11
shampoo:3
soap:5
toothbrush:9
shampoo:3
soap:10
toothbrush:7
shampoo:3
soap:15
toothbrush:5
shampoo:3
soap:20
toothbrush:3
shampoo:3
soap:25
toothbrush:1
shampoo:4
soap:0
toothbrush:8
shampoo:4
soap:5
toothbrush:6
shampoo:4
soap:10
toothbrush:4
shampoo:4
soap:15
toothbrush:2
shampoo:4
soap:20
toothbrush:0
shampoo:5
soap:0
toothbrush:5
shampoo:5
soap:5
toothbrush:3
shampoo:5
soap:10
toothbrush:1
shampoo:6
soap:0
toothbrush:2
shampoo:6
soap:5
toothbrush:0
'''