天天看點

函數小練習(輸出一年的第幾天,将重複的ip位址找出,将字母統一成大寫模式)-python

一:

問題描述

請編寫一個小程式,實作輸入任意年月日,計算出這是這一年的第幾天,如,輸入20210203,則提示這是2021年的第34天

查了一下閏年和平年的資料

1、含義不同。遇到整百年時要被400整除才是閏年,否則為平年;遇到非整百年時,隻要被4整除就是閏年,不能被4整除為平年。

2、天數不同。閏年的2月有29天,平年的2月有28天。

def whichday(y,m,d):
   '''
功能:輸入任意年月,計算這是這一年的哪一天
   offset是為了差別是閏年還是平年
   函數沒有傳回值
'''
   if y%4==0 and y%100!=0:
      offset=1
   else:
      offset=2
   if m==1:
      print('這是一年第'+str(d)+'天')
   if m==2:
      print('這是一年第'+str(31+d)+'天')
   if m==3 or m==5 or m==7:
      print('這是一年第'+(m-1)*30+(m-1)/2+d-offset+'天')
   if m in(4,6,8,10,12):
      print('這是一年第'+(m-1)*30+(m)/2+d-offset+'天')
   
   if m in(9,11):
      print("這是一年第"+(m-1)*30+(m+1)/2+d-offset+'天')
#執行這兩個步驟,會自動調用whichday函數
#if __name__=='__main__':
   #whichday(2021,2,3)
day=str(input('請輸入年月日(如20210203這樣的形式)\n'))
print('輸入的年月日為:',day[:4]+'年',day[5:6]+'月',day[7:]+'日')
whichday(int(day[:4]),int(day[5:6]),int(day[7:]))
           

執行結果

函數小練習(輸出一年的第幾天,将重複的ip位址找出,将字母統一成大寫模式)-python

二:

問題描述

下面list中提供了幾組ip位址,編寫一個函數,将list中重複出現的ip位址找出來

list=[‘192.168.1.1’,‘10.192.66.72’,‘10.199.88.132’,‘192.168.1.1’,‘1.192.168.163’,‘1.2.3’,‘1.2.3’]

‘1.2.3’是我加上去測試的

代碼實作

def find_ip(list):
   l=[]#用來裝沒有重複過的ip
   m=[]#用來裝重複過的ip
   for item in list:#周遊清單
      str_1="".join(item)#将清單轉換為字元串
      if str_1 not in l:
         l.append(str_1)
      else:
         m.append(str_1)
   return m#傳回重複過的ip
list=['192.168.1.1','10.192.66.72','10.199.88.132','192.168.1.1','1.192.168.163','1.2.3','1.2.3']
print('提供的幾組ip位址',list)
print()
print('在list中重複出現的ip位址',find_ip(list))


           

執行結果

函數小練習(輸出一年的第幾天,将重複的ip位址找出,将字母統一成大寫模式)-python

三:

問題描述

編寫一個函數,把輸入的字母統一成大寫模式,如輸入"go big or go home",輸出為"GO BIG OR GO HOME

代碼實作

def printcapital(string):
   print("原來的字元串",string)
   print("将字母統一成大寫模式:",string.upper())
string='go big or go home'
printcapital(string)


           

執行結果

函數小練習(輸出一年的第幾天,将重複的ip位址找出,将字母統一成大寫模式)-python

**************就到這裡啦