2

I'm new to programming and have been trying to get the hang of things when it comes to functions and am starting to explore Tkinter. I followed this tutorial on rock, paper, scissors and I have a reset button that when the user clicks on it, it'll clear the results and what the user inputted as their choice except it seems like it's not clearing the computer's choice i.e computer selects rock, click reset, computer selects rock again, reset, and so on... so I have to click exit instead and run the program to get a different result.

I tried assigning comp_pick = None in the def Reset() and comp_pick.set("") since I clear the user_pick like that. I'm just trying to make sense as to what I missed.

#import library
from tkinter import *
import random

#initialize window set up 
root = Tk()
root.geometry('500x500')
root.resizable(0,0)
root.title('DataFlair-Rock,Paper,Scissors')
root.config(bg ='pink3')


#heading set up 
Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()


##user choice set up 
user_take = StringVar()
Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)



#computer choice using the randint function
comp_pick = random.randint(1,3)
if comp_pick == 1:
    comp_pick = 'rock'
elif comp_pick ==2:
    comp_pick = 'paper'
else:
    comp_pick = 'scissors'
    



##function to play
Result = StringVar()

def play():
    user_pick = user_take.get()
    if user_pick == comp_pick:
        Result.set('It is a tie. You both have selected ' + user_pick +'.') 
    elif user_pick == 'rock' and comp_pick == 'paper':
        Result.set('You loose! Computer selected paper.')
    elif user_pick == 'rock' and comp_pick == 'scissors':
        Result.set('You win! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'scissors':
        Result.set('You loose! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'rock':
        Result.set('You win! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'rock':
        Result.set('You loose! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'paper':
        Result.set('You win! Computer selected paper.')
    else:
        Result.set('invalid: choose any one -- rock, paper, scissors')
    
        
    
##fun to reset
def Reset():
    Result.set("") 
    user_take.set("")
    comp_pick.set("")


##fun to exit
def Exit():
    root.destroy()


###### button
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)

Button(root, font = 'arial 13 bold', text = 'PLAY'  ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)

Button(root, font = 'arial 13 bold', text = 'RESET'  ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)

Button(root, font = 'arial 13 bold', text = 'EXIT'  ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)

root.mainloop()
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
meepmepp
  • 21
  • 2

3 Answers3

1

You are setting comp_pick exactly once, at the start of your program. Instead, do that at the beginning of play to pick a new, random value every time the user plays the game. You're already choosing a new user_pick each time that way, after all.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

The other's have pointed out how you can fix your issue that you are asking about. I'd like to point out one small thing that might lead to problems in the future:

comp_pick = random.randint(1,3)
if comp_pick == 1:
    comp_pick = 'rock'
elif comp_pick ==2:
    comp_pick = 'paper'
else:
    comp_pick = 'scissors'

Here you are using the variable comp_pick to mean two different things. First it is an integer value between 1 and 3. Then you reassign it to a string value for the choice. Reusing a variable for two different meanings like this can easily lead to bugs, so you should avoid doing it. Instead, use two different variables.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

So in your code the place where you have made computer's choice

#computer choice using the randint function
comp_pick = random.randint(1,3)
if comp_pick == 1:
    comp_pick = 'rock'
elif comp_pick ==2:
    comp_pick = 'paper'
else:
    comp_pick = 'scissors'

It is inside the root.mainloop() you must know what .mainloop() does which you can read from here but in short, this mainloop works as a type of event listener & etc so whenever you are resseting the scores your widgets are loaded once again, & with this your computer's choice is also selected as it is there to be executed. So make a function inside the mainloop and before the play() function & call it whenever you need it

def computer_choice():
    #computer choice using the randint function
    choices = ['rock', 'paper', 'scissors']
    comp_pick = random.choice(choices)

And call this function whenever you need, so your whole code should be like this:

#import library
from tkinter import *
import random

#initialize window set up 
root = Tk()
root.geometry('500x500')
root.resizable(0,0)
root.title('DataFlair-Rock,Paper,Scissors')
root.config(bg ='pink3')


#heading set up 
Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()


##user choice set up 
user_take = StringVar()
Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)


##function to play
Result = StringVar()

def computer_choice():
    #computer choice using the randint function
    choices = ['rock', 'paper', 'scissors']
    global comp_pick
    comp_pick = random.choice(choices)

def play():
    computer_choice()
    user_pick = user_take.get()
    if user_pick == comp_pick:
        Result.set('It is a tie. You both have selected ' + user_pick +'.') 
    elif user_pick == 'rock' and comp_pick == 'paper':
        Result.set('You loose! Computer selected paper.')
    elif user_pick == 'rock' and comp_pick == 'scissors':
        Result.set('You win! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'scissors':
        Result.set('You loose! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'rock':
        Result.set('You win! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'rock':
        Result.set('You loose! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'paper':
        Result.set('You win! Computer selected paper.')
    else:
        Result.set('invalid: choose any one -- rock, paper, scissors')
    
        
    
##fun to reset
def Reset():
    Result.set("") 
    user_take.set("")
    comp_pick.set("")


##fun to exit
def Exit():
    root.destroy()


###### button
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)

Button(root, font = 'arial 13 bold', text = 'PLAY'  ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)

Button(root, font = 'arial 13 bold', text = 'RESET'  ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)

Button(root, font = 'arial 13 bold', text = 'EXIT'  ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)

root.mainloop()

Jai Hind, Jai Bharat

Aditya
  • 1,132
  • 1
  • 9
  • 28
  • While your suggestion to make a function is correct, your explanation that "it is inside the `root.mainloop()`" is incorrect. The code for the computer's choice is only run once, not continuously. – Code-Apprentice Dec 16 '20 at 03:36
  • Hmm, @Code-Apprentice well I have updated my answer – Aditya Dec 16 '20 at 04:05