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

Advanced Python Programming -- time date module and exception handling function

編輯:Python

Catalog

  • 1. exception handling
    • 1.1 Capture exception
    • 1.2 Custom exception , Throw an exception
  • 2. Time and date Print log Generate test reports Order


1. exception handling

# Concept :
# The program is running , If the program encounters an error , The behavior that the program will automatically stop , Those that throw error messages are called exceptions 

1.1 Capture exception

# Ensure the robustness and stability of the program 
# Robustness, : health A small problem Get rid of it yourself 
# stability : Let the program run stably 
''' try: Code to execute except: Errors occurred Code to execute Other code '''
# try Put your uncertain code in it 
# except If an error occurs, execute except Code for 
# Input integer 
# num = int(input(" please enter an integer "))
# print(num)
# num1=3+5
# print(num1)
try:
num = int(input(' please enter an integer :'))
print(num)
except:
print(' Please enter the correct number ')
num1=3+5
print(num1)
# Handle Error reporting prompt 
''' try: Code to execute except Wrong type : Wrong handling except Wrong type : Wrong handling '''
# Output a number and divide it by another number 
num = float(input(" Please enter a number :"))
result = 2 / num
print(num)
# ZeroDivisionError: float division by zero
# ValueError: could not convert string to float: 'a'
try:
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
except ZeroDivisionError:
print(' Please don't enter 0,0 Cannot be divided ')
except ValueError:
print(' Please enter the correct number ')
''' try: Code to execute except ( Wrong type 1, Wrong type 2) Wrong handling '''
try:
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
except (ZeroDivisionError,ValueError) as e:
print(' Please enter the correct number %s'%e)
# Interview questions 
# file ioError
# FileNotFoundError
# ValueError
# IndexError
# TypeError Type error 
# I can't think of so many exceptions Rough forecast Encountered other problems What do I do ? Report errors 
# exception Abnormal parent class BaseException
# ZeroDivisionError,ValueError exception Got all the exceptions Can handle 
# exception Accept all exceptions and handle 
# as e The exception information is saved in e Inside as The name 
# Why is it wrong e Error caught 
# e Get exception information 
try:
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
except (ZeroDivisionError,ValueError) as e:
print(' Please enter the correct number %s'%e)
except Exception as e:
print(' Please enter the correct number %s'%e)
''' try: Code to try except Wrong type : Catch error types except Wrong type : Catch error types except Exception as e: print(e) else: Code that will execute without exception finally: It will be carried out anyway '''
try:
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
except ZeroDivisionError as e:
print(' Can not be 0 to be divisible by %s'%e)
except ValueError as e:
print(' Wrong value %s'%e)
except Exception as e:
print(' All the mistakes %s'%e)
else:
print(' Normal execution ')
finally:
print(' Execute with or without exceptions ')
def demo1():
try:
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
except Exception as e:
print(' Please enter the correct number %s' % e)
print(demo1())
def demo1():
num = float(input(' Please enter a number :'))
result = 2 / num
print(result)
try:
print(demo1())
except Exception as e:
print(e)
# Automation can catch exceptions inside functions , You can also catch exceptions when calling functions 
# Call function to catch exception Clean the code 
# base
def open():
try:
driver.get()
except Exception as e:
print(e)
# pom
def login():
try:
driver.on_input()
driver.on_input()
driver.on_click()
except Exception as e:
print(e)

1.2 Custom exception , Throw an exception

# Exception handling can be thrown 
# Custom exceptions are used for specific requirements 
# Input password 
# Password length <8, Throw an exception 
# password >=8, Returns the length of the password 
def input_password():
try:
pwd = input(' Please input a password ')
if len(pwd)<8:
# print(' The password is less than 8')
raise Exception(len(pwd),8)
except Exception as e:
print(' Insufficient password length %s'%e)
else:
print(' Password input complete ')
return pwd
pwd=input_password()
print(pwd)

2. Time and date Print log Generate test reports Order

# Generate calendar Annual calendar, monthly calendar and calendar 
import calendar
# Output 3 The calendar of the month 
cal = calendar.month(2022,3)
print(cal)
# 22 The calendar of the year 
year = calendar.calendar(2022)
print(year)
# Time stamp : 1970 year 1 month 1 Japan 0 spot 0 branch 0 second - Now time seconds 
import time
print(' Current timestamp :',time.time())
# time tuples Tuples ( Mm / DD / yyyy HHM / S What day of the week 0-6 0 It's Monday The day of the year Daylight saving time )
t = (2022,3,13,22,19,21,6,1)
print(t)
# Time stamp to time tuple 
print(' Time stamp to time tuple :',time.localtime(time.time()))
# Time tuple to date time.asctime
print(' Date in English ',time.asctime(time.localtime(time.time())))
# Date format Baidu %Y %m %d Use this more 
print(time.strftime('%Y-%m-%d %H:%M:%S'))

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