728x90

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#Password Generator Project
import random
 
letters = [
    'a''b''c''d''e''f''g''h''i''j''k''l''m''n''o',
    'p''q''r''s''t''u''v''w''x''y''z''A''B''C''D',
    'E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S',
    'T''U''V''W''X''Y''Z'
]
numbers = ['0''1''2''3''4''5''6''7''8''9']
symbols = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
letter_list = [];
for i in range(0, nr_letters):
  random_num = random.randint(0len(letters)-1);
  letter_list.append(letters[random_num]);
 
symbol_list = [];
for i in range(0, nr_symbols):
  random_num = random.randint(0len(symbols)-1);
  symbol_list.append(symbols[random_num]);
 
number_list = [];
for i in range(0, nr_numbers):
  random_num = random.randint(0len(numbers)-1);
  number_list.append(numbers[random_num]);
 
all = letter_list+symbol_list+number_list;
 
random_list = []
 
while len(random_list) < len(all):
    all_len = len(all)
    random_num = random.randint(0, all_len - 1)
    if (random_list.count(random_num) == 0):
        random_list.append(random_num)
 
for i in random_list:
    print(all[i], end="")
 
 
 

1. while len(random_list) < len(all):

    - random_list가 all과 길이가 같아질 때까지 반복

 

2. if (random_list.count(random_num) == 0):

    - count()를 활용하여, 랜덤값 중 중복되지 않은 요소만 random_list에 추가

 

3. for i in random_list:

    - 중복이 제거된 random_list 값을 all에 대입해서 random하게 all list 출력

 

4. print(all[i], end="")

    - end=""를 통해 print 개행 제거

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Password Generator Project
import random
 
letters = [
    'a''b''c''d''e''f''g''h''i''j''k''l''m''n''o',
    'p''q''r''s''t''u''v''w''x''y''z''A''B''C''D',
    'E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S',
    'T''U''V''W''X''Y''Z'
]
numbers = ['0''1''2''3''4''5''6''7''8''9']
symbols = ['!''#''$''%''&''('')''*''+']
 
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
 
pwd_list = [];
 
for char in range(1, nr_letters + 1):
    random_char = random.choice(letters)
    pwd_list += random_char
 
for char in range(1, nr_symbols + 1):
    pwd_list += random.choice(symbols)
 
for char in range(1, nr_numbers + 1):
    pwd_list += random.choice(numbers)
 
random.shuffle(pwd_list);
 
pwd2 = "";
for char in pwd_list:
  pwd2 += char;
  
print(pwd2);
 
 
 

1. random_char = random.choice(letters)

    - choice(): 리스트에서 랜덤하게 요소를 반환

 

2. list에 요소를 추가하는 방법

    - pwd_list += random_char;

    - pwd_list.append(random_char);

 

3. random.shuffle(pwd_list)

    - shuffle(): 리스트 요소 셔플

 

4. pwd2 += char;

    - StringBuffer와 비슷하게 pwd_list 요소를 pwd2에 추가

 

728x90

'Python > Python' 카테고리의 다른 글

[Python_Bootcamp] Data Structures: List  (0) 2023.08.13
[Python_Bootcamp] f-String  (0) 2023.08.08
728x90

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 🚨 Don't change the code below 👇
student_scores = input("Input a list of student scores ").split()
for n in range(0len(student_scores)):
  student_scores[n] = int(student_scores[n])
print(student_scores)
# 🚨 Don't change the code above 👆
 
 
#Write your code below this row 👇
max = 0;
for score in student_scores:
    if max < score:
        max = score;
print(f"The highest score in the class is: {max}");
 
print(max(student_scores));
 
 
 

 

 

🖨️오류

TypeError: 'int' object is not callable

 

 

📡 원인

max()를 사용하기 전에 max 변수를 정의하여 max()를 사용할 수 없게 됨

 

 

📰 해결 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 🚨 Don't change the code below 👇
student_scores = input("Input a list of student scores ").split()
for n in range(0len(student_scores)):
  student_scores[n] = int(student_scores[n])
print(student_scores)
# 🚨 Don't change the code above 👆
 
 
#Write your code below this row 👇
max_score = 0;
for score in student_scores:
    if max_score < score:
        max_score = score;
print(f"The highest score in the class is: {max}");
 
print(max(student_scores));
 
 
 

max 변수 이름을 max_score로 변경하여 max()와 중복되지 않게 함

 

⭐ 내장 함수와 변수명이 중첩되지 않도록 유의

 

728x90
728x90

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
4
5
6
7
position = input("Where do you want to put the treasure? ")
 
col = position[0];
row = position[1];
 
map[row-1][col-1= "X";
 
 
 

 

 

🖨️오류

TypeError: list indices must be integers or slices, not str

 

 

📡 원인

리스트의 인덱스를 정수나 slices가 아닌 String을 사용

 

 

📰 해결 방법

1
2
3
4
5
6
7
position = input("Where do you want to put the treasure? ")
 
col = int(position[0]);
row = int(position[1]);
 
map[row-1][col-1= "X";
 
 
 

String을 int로 변환 후, 리스트의 인덱스로 사용

 

728x90
728x90

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
list8 = ["do""re""mi""mi"];
mi_cnt = list8.count();
 
 
 

 

 

🖨️오류

TypeError: list.count() takes exactly one argument (0 given)

 

 

📡 원인

count()는 1개의 매개변수를 필요로 하는데, 매개변수가 없음

 

 

📰 해결 방법

1
2
3
list8 = ["do""re""mi""mi"];
mi_cnt = list8.count("mi");
 
 
 

count()에 매개 변수로 list에서 개수를 구하고 싶은 요소 입력

 

728x90
728x90

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
list = ["do""re""mi"];
list.remove("pa");
 
 

 

 

🖨️오류

ValueError: list.remove(x): x not in list

 

 

📡 원인

list에 없는 요소를 삭제하려고 함

 

 

📰 해결 방법

1
2
3
list = ["do""re""mi"];
list.remove("mi");
 
 

list에 존재하는 요소를 삭제

 

728x90