1、猜数字游戏
由某人随机出一个指定范围内的数,然后其他人一个一个猜,猜的过程中区间不断缩小,直到猜中为止。
import random
i = int(input("random start:"))
j = int(input("random end:"))
rand = random.randrange(i,j) # 生成区间内的随机数
print('Random number in [' + str(i) + ',' + str(j) + '] generated!')
num = int(input("Please guess the number:"))
count = 0
while(num != rand):
count += 1
if (num < rand):
print('Lower than the answer')
else:
print('Higher than the answer')
num = int(input('Guess the number again:'))
print(' You get the answer with [%d] times' % count)
2、有四个数字1、2、3、4,能组成多少个互不相同且数字不重复的三位数。
count = 1
lst = []
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if (i != k) and (i != j) and (j != k):
lst.append(i*100+j*10+k)
count += 1
print(count)
print(lst)
3、从键盘输入一个字符串,将小写字母全部转换成大写字母,输出到磁盘文件“test”中进行保存。
in_put = input('Please input string:')
new_str = in_put.upper()
with open('test.txt','w') as f:
f.write(new_str)
with open('test.txt','r') as f:
f_str = f.read()
print(f_str)
# >Please input string:ssssAAAKKKssss
# >SSSSAAAKKKSSSS
4、输入3个数,按照从小到大输出
x = int(input('Please input:'))
y = int(input('Please input:'))
z = int(input('Please input:'))
l = [x,y,z]
new_list = sorted(l)
print(new_list)
# >Please input:22
# >Please input:43
# >Please input:55
# >[22, 43, 55]
5、有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
a = 2
b = 1
count = 0
sum_num = 0
while count != 20:
count += 1
sum_num += a/b
t = a+b # 3 # 5
b = a # 2 # 3
a = t # 3
print(sum_num) # 32.66026079864164
a = 2
b = 1
sum_num = 0
for i in range(20):
sum_num += a/b
t = a+b # 3 # 5
b = a # 2 # 3
a = t # 3
print(sum_num) # 32.66026079864164