Home > OS >  why are my if statements not causing the action to be performed
why are my if statements not causing the action to be performed

Time:06-18

i am making a simple rock paper scissors game in python using tkinter, i am taking the data from the entry and comparing it to what the computer has generated to perform a task, howver it is not printing 'draw' in the terminal when i input the same choice as the randomly generated one, and i haven't been able to find any issues with my code.

import tkinter as tk
from tkinter import *
import random
#make window
root = Tk()
root.geometry('400x300')

possible_actions = ['rock','paper','scisors']
computer_action = random.choice(possible_actions)
print(computer_action)


Label1 = Label(root, text="rock paper or scissors")
Label1.place(x=130 ,y=30)

entry1 = Entry(root, width = 20)
entry1.place(x=130, y =70)
entry1.pack
text1 = entry1.get

def button1_command():
if text1 == 'rock' and computer_action == 'rock':
    print('draw')
if text1 == 'scisors' and computer_action == 'scisors':
    print('draw')
if text1 == 'paper' and computer_action == 'paper':
    print('draw')

root.mainloop()

CodePudding user response:

You need to detect when the entry text is edited, then call the button1_command() function:

import tkinter as tk
from tkinter import *
import random
#make window
root = Tk()
root.geometry('400x300')

possible_actions = ['rock','paper','scisors']
computer_action = random.choice(possible_actions)
print(computer_action)


Label1 = Label(root, text="rock paper or scissors")
Label1.place(x=130 ,y=30)

entry1 = Entry(root, width = 20)
entry1.place(x=130, y =70)

def button1_command(e):
    text1 = entry1.get()
    if text1 == 'rock' and computer_action == 'rock':
        print('draw')
    if text1 == 'scisors' and computer_action == 'scisors':
        print('draw')
    if text1 == 'paper' and computer_action == 'paper':
        print('draw')

entry1.bind("<Return>", button1_command)
root.mainloop()

Also, text1 must be a variable that works inside that function, because the input is modified after the variable is instantiated, so if its global it wont update to the correct values.

CodePudding user response:

the solution is pretty easy, you need to use get() instead of get. Also you didn't call the function to check the response... I have re-created your code and added a button which calls the function when clicked.

def button1_command():
    text1 = entry1.get()
    if text1 == 'rock' and computer_action == 'rock':
        print('draw')
    if text1 == 'scisors' and computer_action == 'scisors':
        print('draw')
    if text1 == 'paper' and computer_action == 'paper':
        print('draw')

import tkinter as tk
from tkinter import *
import random
#make window
root = Tk()
root.geometry('400x300')

possible_actions = ['rock','paper','scisors']
computer_action = random.choice(possible_actions)
print(computer_action)


Label1 = Label(root, text="rock paper or scissors")
Label1.pack()

entry1 = Entry(root, width = 20)
entry1.place(x=130, y =70)
entry1.pack()
Button(root, text="Check", command=button1_command).pack()

root.mainloop()

Also you need to fetch the value of text1 inside the function, because if doing it outside, it will store the value of entry widget when it is created (which will be empty as the user hasn't entered anything)...

I have used pack() for simplicity, you can also use place()

  • Related