I want to make a rock-paper-scissors program

Asked 1 years ago, Updated 1 years ago, 111 views

I gathered examples to create a program, but I don't think it's good, so I'm asking questions. The first error that pops up is that com is definedㅠ<

from tkinter import *
import random as r

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


def vs() :
    if com==user :
        print ("It's a tie!" Rock-paper-scissors again!")
    else com==1 :
        if user==2 :
        print("I won! I'll go up 1 space~")
    else com==1 :
        if user==3 :
        print ("I lost...")
    else com==2 :
        if user==1 :
        print ("I lost...")
    else com==2 :
        if user==3 :
        print("I won! I'll go up 3 spaces~")
    else com==3 :
        if user==1 :
        print("I won! I'll go up 2 spaces~")
    else com==3 :
        if user==2 :
        print ("I lost...")

if com==1:
    path1 = PhotoImage (file = "left scissors.gif")
elif com==2:
    path1 = PhotoImage (file = "left rock.gif")
else:
    path1 = PhotoImage (file = "left-hand side.gif")
if var.get()==1:
    path2 = PhotoImage(file = "Right scissors.gif")
elif var.get()==2:
    path2 = PhotoImage (file = "Right Rock"gif")
else :
    path2 = PhotoImage (file = "right-hand side.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 ("Rock-paper-scissors game")

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 = "Choose between scissors, rock and paper.")
lbl2 = Label (frm_mid1, text = "[Computer]")
lbl3 = Label (frm_mid1, text = "[User]")

photo = PhotoImage (file = "first screen.gif")

lblcom = Label(frm_mid2, image=photo)
lbluser = Label(frm_mid2, image=photo)
lblvs = Label(frm_mid2, text = "vs")
lblresult = Label (frm_mid3, text = "What is the outcome of the game?")
var = IntVar()

radio1 = Radiobuton (frm_mid3, text = "scissors", variable = var, value = 1, command = select_img)
radio2 = Radiobuton (frm_mid3, text = "rock", variable = var, value = 2, command = select_img)
radio3 = Radiobuton (frm_mid3, text = "bo", variable = var, value = 3, command = select_img)

window.mainloop()

python random tkinter variable

2022-09-22 11:40

1 Answers

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

The function GBB was declared, not executed. GBB() If you can do it once, I think it will work.

def vs() :
    if com==user :
        print ("It's a tie!" Rock-paper-scissors again!")
    else com==1 :
        if user==2 :
        print("I won! I'll go up 1 space~")
    else com==1 :
        if user==3 :
        print ("I lost...")
    else com==2 :
        if user==1 :
        print ("I lost...")
    else com==2 :
        if user==3 :
        print("I won! I'll go up 3 spaces~")
    else com==3 :
        if user==1 :
        print("I won! I'll go up 2 spaces~")
    else com==3 :
        if user==2 :
        print ("I lost...")

Else, you wrote a logical expression.

This is an incorrect expression. If is not, it means~, so next to else, no factor is taken.

Wouldn't the expression you want be elif(elseif), not else?

??


2022-09-22 11:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.