程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python experiment three functions

編輯:Python

2022.5.25 Afternoon experiment

Experiment three function

List of articles

    • Preface
    • Topic 1
    • Topic two
    • Topic three

Preface

This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement

Topic 1

Write a function func(str), Evaluate and return string str Number in 、 The number of letters and other types of characters

Problem analysis

stay func Use tuples to define numbers in functions 、 Letter 、 Space 、 Other , And initialize to 0. Then use for Loop through characters , successively if Determine which character type it is

Built in functions :

  1. isdigit () Judge the numbers
  2. isalpha () Judging letters
  3. isspace () Judge the space

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
content = input ( ' Input string ' )
def func(s):
num = 0
alpha = 0
space = 0
others = 0
dic = {

'num': 0,
'alpha': 0,
'space': 0,
'other': 0
}
for i in s:
if i.isdigit ():
dic[ 'num' ] += 1
elif i.isalpha ():
dic[ 'alpha' ] += 1
elif i.isspace ():
dic[ 'space' ] += 1
else:
dic[ 'other' ] += 1
return dic
print ( func ( content ) )

result

Topic two

Test Goldbach's conjecture : Any one greater than 2 Even numbers of can be expressed as 2 Sum of prime numbers . Write a function isGDBH(n) Will the incoming 6~100 Even numbers between are expressed as 2 Sum of prime numbers , The results are saved in the list and returned . for example , Function passed in parameters 10, Then return to [“10=3+7”, “10=5+5”]

Problem analysis

Enter a number , Verify that the number entered is within 6~100 Between , call isGDBH(n) Function to validate the data and save the results to a list , Which uses isPrime(n) First determine whether it is a prime number

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
# Judge whether it is a prime 
def isPrime(n):
i=2
while i<=n:
if n%i==0:
break
i+=1
if n==i:
return True
num=int(input(" Enter even number ,6~100 Between \n"))
if num<6:
print(" Please enter greater than 6 An even number of !")
exit(0)
elif num>100:
print(" Please input less than 100 An even number of !")
exit(0)
elif (num%2)>0:
print(" Please enter an even number !")
exit(0)
i=1
result = []
def isGDBH(n):
global i
while(i<=num):
i = i + 1
if(isPrime(i)):
j=1
while(j<num):
j = j + 1
if(isPrime(j) and i<=j): #j<i Prevent duplication 
if(j+i==num):
elem = str(num)+'='+str(i)+'+'+str(j)
result.append(elem)
isGDBH(num)
print(result)

result

Topic three

  1. To write 3 A function , Find triangles separately 、 Perimeter of rectangle and circle
  2. Use a decorator to trim the above 3 The incoming parameters of a function are called and checked for validity

Problem analysis

Design three functions to receive three values of input , Call the respective judgment conditions through the decorator with parameters , By judgment return. The structure of the decorator is messy , You can go and check it out , There is no participation and there is participation , What I use here is a decorator with parameters , With parameters, you need to use internal decorators and internal functions

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 28 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
import math
# Define decorator with parameters 
def DECO(Shape):
# Define interior decorators 
def deco(func):
# Define inner function 
def call_func(a,b=0,c=0):
if(Shape==' triangle '):
# print(' test ')
if ((a + b > c) & (a + c > b) & (b + c > a)):
return func ( a, b, c )
else:
return ' The input is invalid '
if(Shape==' rectangular '):
if((a>0)&(b>0)):
return func(a,b)
else:
return ' The input is invalid '
if(Shape==' circular '):
if(a>0):
return func(a)
else:
return ' The input is invalid '
return call_func
return deco
# Pass decorator parameters 
@DECO(' triangle ')
def Triangle(a,b,c):
return a+b+c
@DECO(' rectangular ')
def Cube(a,b):
return (a+b)*2
@DECO(' circular ')
def Circle(a):
return 2*math.pi*a
if __name__ == "__main__":
a,b,c=map(int,input(' Enter three sides of the triangle ').split(" "))
print(Triangle(a,b,c))
a,b=map(int,input(' Enter both sides of the rectangle ').split(" "))
print(Cube(a,b))
a=int(input(' Enter the radius of the circle '))
print(Circle(a))

result


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved