לדלג לתוכן

6.3 מודולים מובנים שימושיים פתרון

מחרוזת:

import random  
import string  

all_lst = list(string.ascii_letters + string.digits + string.punctuation)  
pass_len = int(input("enter the password`s length: "))  
def pass_gen():  
    while True:  
        pass_list = [random.choice(all_lst) for _ in range(pass_len)]  

        has_lower   = any(c.islower() for c in pass_list)  
        has_upper   = any(c.isupper() for c in pass_list)  
        has_digit   = any(c in string.digits for c in pass_list)  
        has_special = any(c in string.punctuation for c in pass_list)  

        if has_lower and has_upper and has_digit and has_special:  
            pwd = ''.join(pass_list)  
            print(pwd)  
            return pwd  

pass_gen()
  • שימו לב, הפונקציה any בודקת האם קיים לפחות איבר אחד ברשימה נתונה מהסוג הרצוי לנו ומחזירה ערך בוליאני (True/False).

זמן:

import time

def timer(seconds):
    print("Timer started for", seconds, "seconds.")
    time.sleep(seconds)
    print("Timer ended.")

seconds = int(input("Enter the number of seconds for the timer: "))
timer(seconds)

תאריך:

from datetime import datetime

current_datetime = datetime.now()
print("Current Date and Time:", current_datetime)

מערכת הפעלה:

import os

def list_files_in_directory():
    files = os.listdir('.')
    print("Files and directories in the current directory:")
    for file in files:
        print(file)

list_files_in_directory()