본문 바로가기
프로그램/Python 1000제

Python 315제] 두근두근 파이썬(개정판) CHAPTER 10 연습문제 7 p334

by 건티 2024. 10. 7.
728x90

출처 : 반크_독도2023

 

참고풀이]

from tkinter import *
import random

pCount=0
cCount=0
L=["바위","보","가위"]
cSu=random.randrange(3)
comChk=L[cSu]

def Print(p,c,S):
    global pCount, cCount, cSu, comChk
    Str1="사용자 점수 : " + str(pCount)
    Str2="컴퓨터 점수 : " + str(cCount)

    if p != c:
        Str3="사용자=" + str(p) + ", 컴퓨터=" + str(c) + " " + S + " 승리!!!"
    else:
        Str3="사용자=" + str(p) + ", 컴퓨터=" + str(c) + " " + S
        
    L2.config(text=Str1)
    L3.config(text=Str2)
    L4.config(text=Str3)

    cSu=random.randrange(3)
    comChk=L[cSu]

    
#바위:0, 보:1, 가위:2
def Process(pChk,pSu):
    global pCount, cCount, comChk, cSu

    if pChk=="바위" and comChk=="가위":
        pCount+=1
        Print(pSu,cSu,"사용자")
    elif pChk=="바위" and comChk=="보":
        cCount+=1
        Print(pSu,cSu,"컴퓨터")
    elif pChk=="보" and comChk=="가위":
        cCount+=1
        Print(pSu,cSu,"컴퓨터")
    elif pChk=="보" and comChk=="바위":
        pCount+=1
        Print(pSu,cSu,"사용자")
    elif pChk=="가위" and comChk=="보":
        pCount+=1
        Print(pSu,cSu,"사용자")
    elif pChk=="가위" and comChk=="바위":
        cCount+=1
        Print(pSu,cSu,"컴퓨터")
    else:
        Print(pSu,cSu,"비겼습니다!!!")
    

def BaWee():
    Process("바위",0)

def Bo():
    Process("보",1)

def KaWee():
    Process("가위",2)
    
window=Tk()

L1=Label(window, text="가위 바위 보 게임", font=("굴림",38,"bold"))
L1.grid(row=0, column=0, columnspan=3, pady=10)

B1=Button(window, text="바위(0)", bg="orange", font=("굴림",18,"bold"),
                  width=15, command=BaWee)
B2=Button(window, text="보(1)", bg="yellow", font=("굴림",18,"bold"),
                  width=15, command=Bo)
B3=Button(window, text="가위(2)", bg="sky blue", font=("굴림",18,"bold"),
                  width=15, command=KaWee)
B1.grid(row=1, column=0, padx=10, pady=20)
B2.grid(row=1, column=1, padx=10, pady=20)
B3.grid(row=1, column=2, padx=10, pady=20)

L2=Label(window, text="사용자 점수 : 0", font=("굴림",20,"bold"))
L2.grid(row=2, column=0, columnspan=3)

L3=Label(window, text="컴퓨터 점수 : 0", font=("굴림",20,"bold"))
L3.grid(row=3, column=0, columnspan=3)

L4=Label(window, text="사용자=0, 컴퓨터=0, 비겼습니다!!!",fg="orange",
                font=("굴림",22,"bold"))
L4.grid(row=4, column=0, columnspan=3, pady=20)

window.mainloop()

 

참고풀이 결과]

시작화면)

 

실행결과)

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

 

반응형

댓글