🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝
🥰 博客首页:knighthood2001
😗 欢迎点赞👍评论🗨️
❤️ 热爱python,期待与大家一同进步成长!!❤️
目录
约瑟夫生者死者小游戏
30 个人在一条船上,超载,需要 15 人下船。于是人们排成一队,排队的位置即为他们的编号。报数,从 1 开始,数到 9 的人下船。如此循环,直到船上仅剩 15 人为止,
问都有哪些编号的人下船了呢?
people={}
for x in range(1,31):
people[x]=1
# print(people)
check=0
i=1
j=0
while i<=31:
if i == 31:
i=1
elif j == 15:
break
else:
if people[i] == 0:
i+=1
continue
else:
check+=1
if check == 9:
people[i]=0
check = 0
print("{}号下船了".format(i))
j+=1
else:
i+=1
continue
结果如下 :
9号下船了
18号下船了
27号下船了
6号下船了
16号下船了
26号下船了
7号下船了
19号下船了
30号下船了
12号下船了
24号下船了
8号下船了
22号下船了
5号下船了
23号下船了
秒表功能
import time
print('按下回车开始计时,按下 Ctrl + C 停止计时。')
while True:
try:
input() # 如果是 python 2.x 版本请使用 raw_input()
starttime = time.time()
print('开始')
while True:
print('计时: ', round(time.time() - starttime, 0), '秒', end="\r")
time.sleep(1)
except KeyboardInterrupt:
print('结束')
endtime = time.time()
print('总共的时间为:', round(endtime - starttime, 2),'secs')
break
结果如下 :
按下回车开始计时,按下 Ctrl + C 停止计时。
开始
计时: 3.0 秒
计时: 5.0 秒
结束 6.0 秒
总共的时间为: 6.69 secs
获取几天前的时间
import time
import datetime
# 先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
# 转换为时间戳
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
# 转换为其他字符串格式
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)
结果如下 :
2019-05-18 18:06:08
使用正则表达式提取字符串中的 URL
给定一个字符串,里面包含 URL 地址,需要我们使用正则表达式来获取字符串的 URL。
import re
def Find(string):
# findall() 查找匹配正则表达式的字符串
url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', string)
return url
string = 'Nowcoder 的网页地址为:https://www.nowcoder.com,Google 的网页地址为:https://www.google.com'
print("Urls: ", Find(string))
结果如下 :
Urls: ['https://www.nowcoder.com', 'https://www.google.com']
结尾
本文为大家带来几个可能会用到的python基础题,想了解更多,戳这!!