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.