python60题(一)

python60题(一)
一、有n个数字:1,2,3,…,n (3 < n < 10) 从中取3个不同的数字并从小到大排列,打印出所有的取法。
如n=5时,打印 123 124 125 134 135 145 234 235 245 345
In [ ]:

a = 0
b = 0
c = 0
n = int(input(“请从数字3-10输入数字的个数”))
if 3<n<10:
for a in range(1,n+1):
for b in range(1,n+1):
for c in range(1,n+1):
if((a<b)and(a<c)and(b<c)):
print(a,b,c)
else:
print(“无效输入,请从3-10中输入”)

二、输入年月日,输出这是这一年的第几天:
In [ ]:

num=input(“请输入年”)
y=int(num[:4])
pip=input(“月”)
m=int(pip[:2])
cur=input(“day”)
d=int(cur[:2])
print(y,m,d)
#每月天数列表
mon = [31,28,31,30,31,30,31,31,30,31,30,31]
#每月天数列表(闰年)
rmon = [31,29,31,30,31,30,31,31,30,31,30,31]
t=0
if y%40 and y%1000 or y%400==0:
for i in range(m-1):
t+=rmon[i]
else:
for i in range(m-1):
t+=mon[i]
print(“是这一年的第”,format(t+d),“天”)

三、斐波那契数列(Fibonacci sequence),指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。即每个数都等于前两个数之和。
输入一个序号(0开始),输出这个序号对应的值
In [ ]:

n = int(input(“输入斐波那契数列序号”))
a=1
b=0
while b<n:
a,b=b,a+b

print(b,end=’ ')
四、输出 9行9列的99乘法口诀表
In [ ]:

for i in range(1, 10):#1<=i<=9

for j in range(1, i+1):#1<=j<=i

   print('{}x{}={} '.format(j, i, i*j), end='')
print()#每输出一边结束一次换行继续,什么时候结束呢取决于i因为j不能大于i所以当他们相等的时候就结束并且换个行再来

五、实现一个10秒钟的倒计时,即:从10~0依次打印,每次打印间隔一秒
In [ ]:

import time
i = 10
while i >= 0:
print( ‘第{}秒’.format(i),end = ‘’)
i -= 1
time.sleep(1)
后分裂成两个,再过一小时每个细胞又分裂成两个……假设细胞不会死,求第n小时总共的细胞数量
In [ ]:

n=int(input(‘输入小时数’))
x=2
s = 1
while n > 0:
n = n - 1
s = s*x
print(“细菌如今的数量”,s)
七、打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
In [ ]:

for i in range(100,1000):
a=i%10
b=i//10%10
c=i//100
if a3+b3+c**3==i:
print(i,end=" ")
八、输入一串字符,输出空格的个数,字母的个数,数字的个数
In [ ]:

a=0
b=0
c=0
s = input(“输入一串字符”)
for i in s:
if i.isalpha():
a+=1
elif i.isdigit():
b+=1
elif a==’ ':
c+=1

print(“字母的个数为”,a,“数字的个数为”,b,“空格的个数为”,c)
九、有一棵桃树,一只猴子每天都会把桃树上一半的桃子偷走并额外吃一个,第10天猴子发现只剩1个桃子了,求刚开始树上有多少个桃子
In [ ]:

a =10
b=1
while a>1:
b=b+1
b = b*2
a-=1
print(b)

十、田忌赛马策略:故意让最弱的马输给对方最强的马,然后其他两匹马设法取胜。
设有三匹马,能力值分别为10,20,30。输入任意三匹马的能力值,若这三匹马有可能取胜,输出能取胜的三场对阵表,若无论如何都无法取胜,输出“无法取胜”
In [ ]:

horse1 = [10, 20, 30]

h1 = int(input(“请输入第1批马子的能力值”))
h2 = int(input(“请输入第2批马子的能力值”))
h3 = int(input(“请输入第3批马子的能力值”))
h = [h1, h2, h3]

30 30 *

if (h[1] == horse1[2] and h[2] == horse1[2]):
print(str(h[1]) + ‘vs’ + str(horse1[0]))
print(str(h[2]) + ‘vs’ + str(horse1[1]))
print(str(h[0]) + ‘vs’ + str(horse1[2]))
elif (h[0] == horse1[2] and h[1] == horse1[2]):
print(str(h[0]) + ‘vs’ + str(horse1[0]))
print(str(h[1]) + ‘vs’ + str(horse1[1]))
print(str(h[2]) + ‘vs’ + str(horse1[2]))
elif (h[0] == horse1[2] and h[2] == horse1[2]):
print(str(h[0]) + ‘vs’ + str(horse1[0]))
print(str(h[2]) + ‘vs’ + str(horse1[1]))
print(str(h[1]) + ‘vs’ + str(horse1[2]))

20 30 *

elif (h[0] == horse1[1] and h[1] == horse1[2]):
print(str(h[0]) + ‘vs’ + str(horse1[0]))
print(str(h[1]) + ‘vs’ + str(horse1[1]))
print(str(h[2]) + ‘vs’ + str(horse1[2]))
elif (h[0] == horse1[2] and h[1] == horse1[1]):
print(str(h[0]) + ‘vs’ + str(horse1[1]))
print(str(h[1]) + ‘vs’ + str(horse1[0]))
print(str(h[2]) + ‘vs’ + str(horse1[2]))
elif (h[0] == horse1[2] and h[2] == horse1[1]):
print(str(h[0]) + ‘vs’ + str(horse1[1]))
print(str(h[1]) + ‘vs’ + str(horse1[2]))
print(str(h[2]) + ‘vs’ + str(horse1[0]))
elif (h[0] == horse1[1] and h[2] == horse1[2]):
print(str(h[0]) + ‘vs’ + str(horse1[0]))
print(str(h[1]) + ‘vs’ + str(horse1[2]))
print(str(h[2]) + ‘vs’ + str(horse1[1]))
elif (h[1] == horse1[2] and h[2] == horse1[1]):
print(str(h[0]) + ‘vs’ + str(horse1[2]))
print(str(h[1]) + ‘vs’ + str(horse1[0]))
print(str(h[2]) + ‘vs’ + str(horse1[1]))
elif (h[1] == horse1[1] and h[2] == horse1[2]):
print(str(h[0]) + ‘vs’ + str(horse1[2]))
print(str(h[1]) + ‘vs’ + str(horse1[1]))
print(str(h[2]) + ‘vs’ + str(horse1[0]))

else:
print(“无法取胜”)

十一、输入任意一个数,输出这个数各个位上的数字
In [ ]:

a=int(input(“请输入:”))
i=0
while a>0:
a1 = a%10
a = int(a/10)
i+=1
print(“a的第{}位为:{}”.format(i,a1))

十二、有一个排好序的列表3 9 12 24 33 41 48 56 69 72 88 90,输入一个数,插入到列表中,输出新的列表,要求保持从小到大的顺序
In [ ]:

nums = [3,9,12,24,33,41,48,56,69,72,88,90]
n = int(input(“输入一个数”))
s = []
s.append(n)
s = s+ nums
s.sort()
print(s)

十三、输出你的电脑计算从1加到100万使用的时间
In [ ]:

import time
start_time=time.time()

n=1000000
s=0
while n>0:
s=s+1
n-=1

stop_time=time.time()
zong=stop_time-start_time
print(zong,‘秒’)
十四、猜码游戏: 每一轮里,程序随机生成两个数字,一个是码数,范围:0到5,一个是猜数,范围:码数到10。用户输入两个数字,也分为码数和猜数。
若这一轮程序的猜数等于两个码数之和,输出“电脑胜”,若用户的猜数等于两个码数之和,输出“你胜”
若都没猜对或都猜对了,公布双方码数,继续下一轮,直到出胜负为止
如:电脑5 8 你 2 7 你胜,电脑5 8 你 3 7 电脑胜,电脑5 8 你 3 8 同时胜,电脑5 7 你 3 6 无人胜
In [ ]:

import random
I = 0
while I <1:
a = int(input(‘请输入码数’))
b = int(input(‘请输入猜数’))
i = random.randint(0, 6)
j = random.randint(0, 11)
print(“电脑码数及猜数”, i, j)

if a + i == b and b > j or b < j:
    I = 1
    print("你胜")
elif a + i == j and b > j or b < j:
    I = 1
    print("电脑胜利")
elif a + i == b == j:
    I = 1
    print("都胜")
else:
    I = 0
    print("无人取胜")

十五、超市购物结账,首先问顾客是否是会员,如果是,所有商品打8折,如果不是,如果购买超过200元商品,则减免20元,否则无优惠
输入购买的商品总价、是否会员,输出实际需要支付的金额
In [ ]:

price = int(input(“输入商品总价”))
vip = int(input(“是否会员?1 是 0 否”))
if vip1:
price
price*0.8
print(“应支付”,price,“元”)
elif vip0 and price<=200:
print(“应支付”,price,“元”)
elif vip
0 and price>200:
price=price-20
print(“应支付”,price,“元”)
十六、银行ATM流水日志保存。启动程序时,可以选择存款、取款操作。每次操作会把流水记录下来,如存100记成 +100 ,取50记成 -50,操作结束后,把所有的记录写到一个文件里,下次执行时继续使用
In [ ]:

import os
#还没有创建过日志,首先创建
if not os.path.exists(“C:/流水.txt”):
f = open(“D:/流水.txt”,‘w’)
f.close()
#读取上次保存好的日志内容
f = open(“C:/流水.txt”,‘r’)
log = f.read()
f.close()
#进行新的操作
while 1:
a = int(input(“请选择操作:1存款 2取款 0结束”))
if a == 0:
break
elif a1:
s = input(“存多少”)
log +=’+’+ s
elif a
2:
s = input(“取多少”)
log +="-"+s
f = open(“D:/流水.txt”,“w”)
f.write(log)
f.close()
十七、有一张成绩表,规定60分以下为E级,60-69分为D级,70-79分为C级,80-89分为B级,90-100分为A级,输出各个级别的人数
In [ ]:

scores = [65, 48, 75, 90, 85, 45, 88, 86, 78, 87, 85, 56, 92, 95, 71, 96, 68, 76, 85, 84, 79, 84, 90, 80, 69]
j = 0
a = 0
b = 0
d = 0
for i in scores:
if 60 <= i <= 69:
j += 1
elif 70 <= i <= 79:
a += 1
elif 80 <= i <= 89:
b += 1
elif 90 <= i <= 100:
d += 1

print(“等级为A的人有”, d, “等级为B的人”, b, “等级为C的人”, a, “等级为D的人”, j)

十八、统计一句英文中每个单词出现的次数,标点符号需要排除
In [ ]:

text = “Hello Tom, Hello Mario, Hello Sam. Let’s play Super Mario.”
text = text.replace(’,’,’ ‘).replace(’.’,’ ')
word=text.split()
d={}
for i in word:
if i in d:
d[i]+=1
else:
d[i]=1

print(d)

十九、某农村家庭种植稻谷,平均每半年收割一次,平均每次收获300公斤大米,一家人每个月吃30公斤大米,当大米囤积超过1吨时,卖掉500公斤。用多线程模拟这个过程(可用1秒表示一个月,一个变量表示当前的大米存量)
In [ ]:

import threading
import time
rice = 0
i=0
def produce():
global rice
while 1:
print(“收割大米”)
rice=rice+300
if rice>=1000:
print(“卖掉大米”)
rice=rice-500
time.sleep(6)

def eat():
global rice
while 1:
time.sleep(1)
print(“吃饭”)
rice=rice-30

t1=threading.Thread(target=produce)

t2=threading.Thread(target=eat)
t1.start()
t2.start()

二十、一家粉店,只有一个店主,店主每3秒煮出一碗粉。
10秒后,张三、李四、王五到店吃粉,三个人吃粉的速度分别为5秒、6秒、7秒一碗,且吃完接着吃下一碗,直到没有粉为止,结束循环
店主今天总共只做30碗粉,做完则结束循环
In [ ]:

import threading
import queue
import time
i = 1
b = 1
c = 1
a = 1
d = 1
def produce():
global i,b,c,a,d
while 1:
if i <=30:
print(“正在制作了第{}碗面条”.format(i))
time.sleep(3)
print(“制作了{}碗面条”.format(b))
b = b + 1
i += 1

def eat1():
time.sleep(10)
global i,b,c,a,d
while 1:
print(“张三吃了第{}碗面条”.format©)
c += 1
b -= 1
time.sleep(5)

def eat2():
time.sleep(10)
global i,b,c,a,d
while 1:
print(“李四吃了第{}碗面条”.format(a))
a += 1
b -= 1
time.sleep(6)

def eat3():
time.sleep(10)
global i,b,c,a,d
while 1:
print(“王五吃了第{}碗面条”.format(d))
d += 1
b -= 1
time.sleep(7)
t1 = threading.Thread(target=produce)
t2 = threading.Thread(target=eat1)
t3 = threading.Thread(target=eat2)
t4 = threading.Thread(target=eat3)
t1.start()
t2.start()
t3.start()
t4.start()

In [ ]:
解析总结
if #nums[i] < n and n <= #nums[i+1]:
#s.append(nums[i])先把前面的#i加起来
#s.append(n)#再把n加在后面
#s.append(nums[i])#这个是在if结束后的,但是for还在执行呢而此时s以及有n的取值了,所以重新将#i一个个加入的时候不会加重复的

for i in range(m-1):#这里代表i取代从0到m-1
t+=rmon[i]#每个个相加

print(“是这一年的第”,format(t+d),“天”)

import time
start_time=time.time()
#函数
stop_time=time.time()
总时间=stop_time-start_time
time.sleep(5)#休息五秒

import os
#还没有创建过日志,首先创建
if not os.path.exists(“C:/流水.txt”):
f = open(“D:/流水.txt”,‘w’)
f.close()
#读取上次保存好的日志内容
f = open(“C:/流水.txt”,‘r’)
log = f.read()
f.close()
#进行新的操作
f = open(“D:/流水.txt”,“w”)
f.write(log)
f.close()

import threading#调用线程
import time
rice = 0
def produce():
global rice#global可以衍生到下一个函数
while 1:
def eat():
global rice
while 1:
t1=threading.Thread(target=produce)#调用函数
t2=threading.Thread(target=eat)
t1.start()
t2.start()#执行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南师大蒜阿熏呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值