Python small topic 3

(1) Pause the output for one second and format the current time.
(2) Classical problem: There is a pair of rabbits, and a pair of rabbits will be born every month from the third month after birth, and a pair of rabbits will be born every month after the third month. If the rabbits are both Undead, what is the total number of rabbits per month?
(3) Determine how many prime numbers there are between 101 and 200, and output all prime numbers.

first question:

#暂停一秒输出,并格式化当前时间。
import time

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
#time localtime() 函数类似gmtime(),作用是格式化时间戳为本地的时间。
#如果sec参数未输入,则以当前时间为转换标准。 DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时。
time.sleep(1)
#停止1秒
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
#time strftime() 函数用于格式化时间,返回以可读字符串表示的当地时间,格式由参数 format 决定。
time.sleep(1)

print(time.ctime())   
# time ctime() 函数把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。 
# 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于 asctime(localtime(secs))。

time.sleep(1)

print(time.asctime(time.localtime(time.time())))
#time asctime() 函数接受时间元组并返回一个可读的形式为
# "Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。

The second question (this is a Fibonacci sequence, using recursion):

#古典问题:有一对兔子,从出生后第3个月起每个月都生一对小兔子,
# 小兔子长到第三个月后每个月又生一对小兔子,假如兔子都不死,每个月的兔子总数为多少?

i = int(input("请输入月份:") )

def tuzi(n):
    if n == 1 or n == 2:
        return 1
    else:
        return tuzi(n - 1) + tuzi(n - 2)

print("%d月后兔子有%d对" %(i,tuzi(i)))

The third question:

#判断101~200之间有多少个素数,并输出所有素数。

sum = 0

for i in range(101,200):
    for j in range(2,i):
        if i % j == 0:
           break
    else:
        print("%d是素数" %i)
        sum += 1
print("一共有%d个素数!!!" %sum)

Guess you like

Origin blog.csdn.net/weixin_43635067/article/details/128787785