Python/개념

2024-03-18 과제 가위바위보

nomad06 2024. 3. 18. 15:41

과제 1

가위. 바위, 보 게임 만들기
- 가위, 바위, 보 중 하나를 입력하세요: 가위
- 컴퓨터: 바위, 유저: 가위 -> 결과: 컴퓨터 승!

import random # random.py

user = input('가위, 바위, 보 중 하나를 입력하세요: ')
pc = random.randint(1, 3)  # 1==가위 2==바위 3==보

def c_name(pc):
    if pc ==1:
        return  '가위'
    elif pc ==2:
        return '바위'
    elif pc ==3:
        return '보'
pc = c_name(pc)

def game(user, pc):
    if user == '가위':
        if pc == '가위':
            return '비겼습니다'
        elif pc == '바위':
            return '컴퓨터 승!'
        elif pc == '보':
            return '유저 승!'
    elif user == '바위':
        if pc == '가위':
            return '유저 승!'
        elif pc == '바위':
            return '비겼습니다'
        elif pc == '보':
            return '컴퓨터 승!'
    elif user == '보':
        if pc == '가위':
            return '컴퓨터 승!'
        elif pc == '바위':
            return '유저 승!'
        elif pc == '보':
            return '비겼습니다'
    else:
        return '잘못입력하셨습니다.'

game1 = game(user, pc)

print(f'컴퓨터: {pc}, 유저: {user} -> 결과: {game1}')
가위, 바위, 보 중 하나를 입력하세요: 보
컴퓨터: 보, 유저: 보 -> 결과: 비겼습니다

 

 

과제 2

로또 예측 프로그램을 작성해보자

- 1 ~ 45까지의 임의의 수 6개를 출력
- 중복된 숫자가 없어야 함
- 오름차순으로출력

import random # random.py

num = []

def ran(li):   # 랜덤 6개
    for i in range(6):
        li.append(random.randint(1, 45))
        li.sort()  #오름차순
    return li

num = ran(num)
print('1===>',num)

# 중복제거
def overlap(li):
    while  len(set(li))  !=  6: 
        li = []
        li = ran(li)
    return li

num = overlap(num)

print(f'예상 번호는: {num}')
예상 번호는: [15, 16, 21, 26, 28, 35]

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

2024-03-19 파이썬 스페셜(매직) 메소드  (0) 2024.03.19
2024-03-19 파이썬 상속  (1) 2024.03.19
2024-03-18 객체지향과 클래스  (0) 2024.03.18
2024-03-18 콜백함수와 람다함수  (0) 2024.03.18
2024-03-18 변수의 범위  (0) 2024.03.18