가위바위보 프로그램을 만들고 싶어요

조회수 3372회

프로그램을 만들려고 예제들을 이리저리 모았는데 영 아닌 것 같아서 질문 올려봐요.. 제일 처음 뜨는 오류는 com이 define 된다고 하네요ㅠㅠ

from tkinter import *
import random as r

def GBB() :
    global com
    com = random.randint(1,3)


def vs() :
    if com==user :
        print("비겼군요! 다시 가위바위보!")
    else com==1 :
        if user==2 :
        print("이겼어요! 1칸 올라갈게요~")
    else com==1 :
        if user==3 :
        print("졌어요..")
    else com==2 :
        if user==1 :
        print("졌어요..")
    else com==2 :
        if user==3 :
        print("이겼어요! 3칸 올라갈게요~")
    else com==3 :
        if user==1 :
        print("이겼어요! 2칸 올라갈게요~")
    else com==3 :
        if user==2 :
        print("졌어요..")

if com==1:
    path1 = PhotoImage(file = "왼쪽가위.gif")
elif com==2:
    path1 = PhotoImage(file = "왼쪽바위.gif")
else:
    path1 = PhotoImage(file = "왼쪽보.gif")
if var.get()==1:
    path2 = PhotoImage(file = "오른쪽가위.gif")
elif var.get()==2:
    path2 = PhotoImage(file = "오른쪽바위.gif")
else :
    path2 = PhotoImage(file = "오른쪽보.gif")

lblcom.config(image = path1)
lblcom.image = path1
lbluser.config(image = path2)
lbluser.image = path2

com = r.randint(1,3)

window = tkinter.Tk()
window.title("가위바위보 게임")

frm_top = Frame(window)
frm_mid = Frame(window)
frm_mid1 = Frame(frm_mid)
frm_mid2 = Frame(frm_mid)
frm_mid3 = Frame(frm_mid)
frm_top.pack(side = "top", fill = "both")
frm_mid.pack(side = "top", fill = "both")
frm_mid1.pack(side = "top", fill = "both")
frm_mid2.pack(side = "top", fill = "both")
frm_mid3.pack(side = "bottom", fill = "both")

lbl1 = Label(frm_top, text = "가위, 바위, 보 중 하나를 선택하세요.")
lbl2 = Label(frm_mid1, text = "[컴퓨터]")
lbl3 = Label(frm_mid1, text = "[사용자]")

photo = PhotoImage(file = "첫화면.gif")

lblcom = Label(frm_mid2, image=photo)
lbluser = Label(frm_mid2, image=photo)
lblvs = Label(frm_mid2, text = "vs")
lblresult = Label(frm_mid3, text = "승부의 결과는?")
var = IntVar()

radio1 = Radiobutton(frm_mid3, text = "가위", variable = var, value = 1, command = select_img)
radio2 = Radiobutton(frm_mid3, text = "바위", variable = var, value = 2, command = select_img)
radio3 = Radiobutton(frm_mid3, text = "보", variable = var, value = 3, command = select_img)

window.mainloop()

1 답변

  • def GBB() :
        global com
        com = random.randint(1,3)
    

    GBB라는 함수는 선언이 되었지, 실행이 되지 않았습니다. GBB() 한번 실행 해 주시면, 될 것 같습니다.

    def vs() :
        if com==user :
            print("비겼군요! 다시 가위바위보!")
        else com==1 :
            if user==2 :
            print("이겼어요! 1칸 올라갈게요~")
        else com==1 :
            if user==3 :
            print("졌어요..")
        else com==2 :
            if user==1 :
            print("졌어요..")
        else com==2 :
            if user==3 :
            print("이겼어요! 3칸 올라갈게요~")
        else com==3 :
            if user==1 :
            print("이겼어요! 2칸 올라갈게요~")
        else com==3 :
            if user==2 :
            print("졌어요..")
    

    else 다음에, 논리식을 써주셨는데.

    이는 잘못된 식입니다. if가 아니면~ 이라는 뜻이므로 else 다음으로는 어떠한 인자도 취하지 않습니다.

    원하시는 식은, else가 아니라, elif(else if)가 아닐까요?

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)