상세 컨텐츠

본문 제목

장고마무리_어깨힘 풀고 파이썬

파이썬·장고·루비·알고리즘

by 김일국 2019. 6. 1. 18:30

본문

장고(파이썬) 마무리 포스트로

어깨힘 풀고 파이썬으로 한번 가위(Scissors),바위(Rock),보(Paper) 게임 이후 rps게임으로 명칭. 을 만들어 보았습니다.

파이썬으로 웹사이트를 만들때 필요한 기초 사용법을 익혀 보았습니다.(아래 사진과 내용)


rps.py 파일 내용(아래, 모듈, 한줄주석, 여러줄 주적, 함수정의, 리스트, 튜플, 딕셔너리, 반복문, 조건문 등이 모두 사용됩니다.)

import random #모듈(함수모음) 불러오기

rps = ['Rock','Paper','Scissors']
#딕셔너리로 구현
results = {
    ('Scissors','Paper'):True,
    ('Scissors','Rock'):False,
    ('Rock','Scissors'):True,
    ('Rock','Paper'):False,
    ('Paper','Rock'):True,
    ('Paper','Scissors'):False
    }
#함수 정의
def compare(p_choice, c_choice):
    #print(p_choice, c_choice) #디버그
    #print(results.get((p_choice,c_choice))) #디버그
    if p_choice == c_choice:
        print('player Tied!')
    elif results.get((p_choice,c_choice))==True:
        print('player Won!')
    elif results.get((p_choice,c_choice))==False:
        print('player Lost!')
    else:
        print('Choise', rps, 'Or For Finished: End')

while True:
    player = input('Rock/Paper/Scissors/End: ')
    computer = random.choice(rps)

    if player=="End":
        break

    print(player, computer)
    # if condition
    """
    if player==computer:
        print('Tied!')
    elif player=='Scissors':
        if computer=='Rock':
            print('Lost!')
        else:
            print('Won!')
    elif player=='Rock':
        if computer=='Paper':
            print('Lost!')
        else:
            print('Won!')
    elif player=='Paper':
        if computer=='Scissors':
            print('Lost!')
        else:
            print('Won!')
    else:
        print('Choise', rps, 'Or For Finished: End')
    """
    compare(player, computer) #함수 호출

    """ # tuple, dictionary use
    if player==computer:
        print('Both Tied!')
    elif results[(player,computer)]:
        print('player Won!')
    else:
        print('player Lost!')
    """

관련글 더보기

댓글 영역