Language/Python

module, random, max, password

원2 2021. 6. 28. 12:31
728x90
반응형

as 로 간단하게 줄이기 가능

import random
import my_module as my

random_side =  random.randint(0,1)
print(random_side)

if random_side == 1 :
    print("앞면")
else :
    print("뒷면")

print(my.pi)


 

랜덤으로 인덱스값 추출해서 점심내기

 

split은 " " 안의 입력값을 잘라준다

 

import random

names_string = input("내기를 할 친구들의 이름을 적습니다. 콤마(,)로 분리해서 적어주세요 : ")
names = names_string.split(",")

ran = random.randint(0,len(names)-1)

print(f"오늘의 점심은 {names[ran]} 님이 쏩니다!")

가위바위보

import random

바위 = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

보 = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

가위 = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''
game_images = [가위,바위,보]

user_choice = int(input("가위는 0 바위는 1 보는 2 를 적어 주세요.\n"))
print(game_images[user_choice])

computer_choice = random.randint(0, 2)
print("컴퓨터 선택:")
print(game_images[computer_choice])

if user_choice >= 3 or user_choice < 0: 
  print("잘못된 번호를 선택했어요 0 , 1, 2 중 선택하세요")
elif ( user_choice == 0 and computer_choice ==2          ):
  print('You win!')
elif ( computer_choice == 0  and user_choice == 2           ):
  print('You lose!')
elif ( user_choice > computer_choice          ):
  print('You win!')
elif ( user_choice < computer_choice            ):
  print('You lose!')
elif ( user_choice == computer_choice            ):
  print('Draw')
else :
    print("Draw")

 

# 🚨 여기는 그대로 👇
student_heights = input("학생들의 키를 입력하세요\n").split()
# split() => 기본공백(스페이스)로 입력된 값을 리스트로 저장
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])
# print(student_heights)
# 🚨 여기는 그대로 👆

# 아래에 코드 작성 👇

total_height = 0
for height in student_heights:
  total_height += height
print(f"전체 키의 합 = {total_height}")

total = len(student_heights)
print(f"학생 수 = {total}")

total_div = total_height/len(student_heights)

print(f"전체 키의 평균 = {total_div}")

최대값

최솟값은 가장 큰 수와 비교해서 반대로 ㄱㄱ

# 🚨 여기는 그대로 👇
student_scores = input("학생들의 성적을 입력 :\n").split()
for n in range(0, len(student_scores)):
  student_scores[n] = int(student_scores[n])
  #성적 입력을 문자열로 받기 때문에 다시 정수로 변환해서 리스트에 입력
print(student_scores)
# 🚨 여기는 그대로 👆

# 아래에 코드 작성 👇
highest_score = 0

for score in student_scores:
    if score > highest_score :
        highest_score = score

# highest_score 와 들어오는 값을 계속 비교하면서 값을 갱신해나감

print(f"가장 높은 점수는 : {highest_score}")

 

패스워드 만들기

#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("파이썬 비밀번호 생성기!")
nb_letters = int(input("몇개의 영문자? ")) 
nb_symbols = int(input("몇개의 특수문자? "))
nb_numbers = int(input("몇개의 숫자? "))

# 패스워드를 저장할 리스트를 생성 
password_list = []

# 영문자 개수만큼 반복문
for i in range(1, nb_letters + 1):
  password_list.append(random.choice(letters))
  #비밀번호 리스트에 문자열리스트에서 랜덤으로 하나 추가


# 아래에 특수문자와 숫자도 패스워드 리스트에 추가하는 코드 작성 👇
for ii in range(1, nb_symbols +1) :
    password_list.append(random.choice(symbols))


for iii in range(1, nb_numbers +1) :
    password_list.append(random.choice(numbers))



# 위에 특수문자와 숫자도 패스워드 리스트에 추가하는 코드 작성 👆

print(password_list)
# 셔플하면 리스트의 순서를 랜덤으로 재설정
random.shuffle(password_list)
print(password_list)

password = ""
for char in password_list:
  password += char

print(f"생성된 패스워드 : {password}")
728x90
반응형

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

예제 21-30  (0) 2021.06.30
예제 11-20  (0) 2021.06.30
예제 01-10  (0) 2021.06.30
while 문, for 문  (0) 2021.06.28
if, elif, else  (0) 2021.06.25
내장함수 function  (0) 2021.06.25