파이썬/기본

[파이썬][응용]가위바위보게임 - random() 공부

caramel-bottle 2023. 9. 11.

 

import random

ls = ['가위', '바위', '보'] # 가위: 0, 바위: 1, 보: 2

while True:
  com = int(random.random() * 3)
  # print(com, ls[com])
  me = input("가위, 바위, 보 중 하나를 선택하세요: ")
  if ls.index(me) == com:

    result = "비겼습니다."
    print(f'컴퓨터: {ls[com]}, 나: {me} -> {result}')
  else:
    if ((ls.index(me) == 0 and com == 1) or
        (ls.index(me) == 1 and com == 2) or
        (ls.index(me) == 2 and com == 0)):
      result = "졌습니다."
      print(f'컴퓨터: {ls[com]}, 나: {me} -> {result}')

    else:
      result = "이겼습니다."
      print(f'컴퓨터: {ls[com]}, 나: {me} -> {result}')
      print("프로그램을 종료합니다.")
      break

 

'파이썬 > 기본' 카테고리의 다른 글

[파이썬] 사용자 정의 함수  (0) 2023.09.11
[파이썬][응용]로또번호 추천 - random() 공부  (1) 2023.09.11
[파이썬] 랜덤 모듈  (0) 2023.09.11
[파이썬] 컬렉션과 반복  (0) 2023.09.11
[파이썬] 반복문  (0) 2023.09.11

댓글