Python, rock-paper-scissors

Asked 2 years ago, Updated 2 years ago, 13 views

from tkinter import *

import random


com = {'scissors', 'rock', 'paper'}

user = {'scissors', 'rock', 'paper'}


def rcp_game( ) :



    com = random.choice (['scissors', 'rock', 'paper'])
    user = ('Scissors', 'Rock', 'Paper')

    if (user=='scissors' and com=='paper') or (user=='rock' and com=='scissors') or (user=='paper' and com=='rock'):
        lbl2.configure(text="user win")
        lbl3 = Label(root, image = photo1)
        lbl3.place(x=100, y=30) 
    elif (user=='scissors' and com=='scissors') or (user=='rock' and com=='rock') or (user=='paper' and com=='paper'):
        lbl2.configure(text="No win")
        lbl3 = Label(root, image = photo2)
        lbl3.place(x=100, y=30)
    elif (user=='scissors' and com=='rock') or (user=='rock' and com=='paper') or (user=='paper' and com=='scissors'):
        lbl2.configure(text="computer win")
        lbl3 = Label(root, image = photo3)
        lbl3.place(x=100, y=30)
    else:
        lbl2.configure (text="re-enter")
        lbl3 = Label(root, image = photo2)
        lbl3.place(x=100, y=30)

root = Tk()
root.title ("rock-paper-scissors")
root.geometry("230x120")

photo1 = PhotoImage(file = "smile.png")

photo2 = PhotoImage(file = "try.png")

photo3 = PhotoImage(file = "cry.png")



lbl1 = Label (root, text="user")

ent =Entry(root)

lbl2 = Label (root, text = "win or lose")

btn = Button (root, text = "OK", command = rcp_game)

lbl1.place(x= 10, y=10)



ent.place(x=50, y=10)
btn.place(x=50, y=40)
lbl2.place(x=50, y=80)

root.mainloop()

Only the else part is executed here. It just means the program isn't working. Why is that?

python

2022-09-21 09:58

1 Answers

Yes, that's right. Currently, no if interval of rcp_game() is executed. Why?

def rcp_game( ) :
    com = random.choice (['scissors', 'rock', 'paper'])
    user = ('Scissors', 'Rock', 'Paper')

    # At this point, the user is just a tuple

    if (user=='scissors' and com=='paper') or (user=='rock' and com=='scissors') or (user=='paper' and com=='rock'): 

        # Even at this point, the user is just a tuple, so it cannot be true when compared to the string 'scissors'

rcp_game() will need to receive the user's choice (scissors or rocks, etc.) as a factor. Like rcp_game(userInput).

Please try again.


2022-09-21 09:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.