@@ -55,56 +55,7 @@ def my_func(a): # moved a from global variable to an argument
5555# function, making it more flexible and easier to use than if 'a' were hard-coded.
5656
5757
58-
59- # Question 3: Write a function that can generate a random password from
60- # upper-case and lower-case letters, numbers, and special characters
61- # (!@#$%^&*). It should have an argument for password length, and should
62- # check to make sure the length is between 8 and 16, or else warn the user
63- # and exit. Your function should also have a keyword argument named
64- # "special_chars" that defaults to True. If the function is called with the
65- # keyword argument set to False instead, then the random values chosen should
66- # not include special characters. Create a second similar keyword argument
67- # for numbers. Use one of the two libraries below.
68-
69- import random
70- # from numpy import random
71-
72- # creating the function
73- def pass_func (length , special = True , num = False ): # adding in args and kwargs
74- length = int (length ) # making length an integer
75- letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # defining letters
76- numbers = '123456789' # defining numbers
77- special_chars = '!@#$%^&*' # defining special characters
78- while True :
79- if length < 8 or length > 16 : # if inputted length is less than 8 or more than 16
80- print ("Sorry, too short." ) # print this
81- break # and end the function
82- if length >= 8 or length <= 16 : # if the length is 8-16 then
83- # apply these conditions depending on special and num arguments
84- if special == True and num == False :
85- alphabet = letters + special_chars
86- # .join joins together the random symbols chosen with random.choice()
87- password = '' .join (random .choice (alphabet ) for i in range (length ))
88- elif special == True and num == True :
89- alphabet = letters + special_chars + numbers
90- password = '' .join (random .choice (alphabet ) for i in range (length ))
91- elif special == False and num == True :
92- alphabet = letters + numbers
93- password = '' .join (random .choice (alphabet ) for i in range (length ))
94- elif special == False and num == False :
95- alphabet = letters
96- password = '' .join (random .choice (alphabet ) for i in range (length ))
97- return password
98-
99-
100- # testing out the function
101- pass_func (8 ) # with only the length argument + defaults
102- pass_func (8 , True , True ) # with letters + special chars + numbers
103- pass_func (12 , False , True ) # with only letters + numbers
104- pass_func (16 , False , False ) # with only letters
105-
106-
107- # Question 4: Create a class that requires four arguments when an instance
58+ # Question 3: Create a class that requires four arguments when an instance
10859# is created: one for the person's name, one for which COVID vaccine they
10960# have had, one for how many doses they've had, and one for whether they've
11061# ever had COVID. Then create instances for four people:
0 commit comments