A collection of exercises for Python beginners(Q3:lack perseverance)

A collection of exercises for Python beginners(Q3)

As a Python beginner, I’ve done a lot of practice. Most of these exercises are very interesting. In order for the other beginners to get to learn the basic usage of the language faster, I have compiled the results of my exercises for your reference. As I am a foreign student, this exercise is written in English. Most of the exercises come from 《C语言初学者趣味编程100例》(贾蓓/郭强/刘占敏). A small number of cases come from the Internet. If any case violates your rights, please tell me, and I will delete it as soon as possible.

Q3:lack perseverance

Question:
There is a saying in China, “fish for three days and lay the net for two days.”, which is used to describe people lack of perseverance. From January 1, 1990, someone began to “fish for three days and lay the net for two days.” He was asked whether he would fish or sun the net on a later day. (Take August 1, 2020 as an example)
Python Code:

def Days(y,m,d):
   list1=[31,28,31,30,31,30,31,31,30,31,30,31]
   a=0
   for i in range(1990,y):
      if (i%4==0 and i%100!=0) or i%400==0:
         a=a+366
      else:
         a=a+365
   for j in range(m-1):
      a=a+list1[j]
   if ((y%4==0 and y%100!=0) or y%400==0) and m>=3:
      a=a+1
   a=a+d
   return a

list1=['lay the net','fish','fish','fish','lay the net']
y=2020
m=8
d=1
n=Days(y,m,d)

print(y,'.',m,'.',d,list1[n%5])

Running Results:
2020 . 8 . 1 打鱼
Brief analysis:
The code above defined the function Days to calculate the number of days since January 1, 1990. For example, there are 3 days from January 1, 1990 to January 3, 1990. There are 11171 days from January 1, 1990 to August 1, 2020. Then according to the regular pattern to calculate whether it’s a day for fishing or laying the net. The core of the function is the calculation of the leap year which can be divisible by 4 and can’t be divisible by 100 or can be divisible by 400.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值