mardi 23 décembre 2014

Trying to find a character in a string, Python using Tkinter

I'm currently making a hangman game for my school project and have hit a snag. I'm trying to test my code and see if I can make it output yay in the console if the character you press on the GUI is correct, and output boo if not. I've tried a try, except and an if else, and the if else says that 'text' is not defined. (Sorry about the block with the buttons, I'm going to clean that up soon!!!) Here's the code:



#Hangman

from tkinter import *
import random

root = Tk()


word_list = ["APPLE", "PEAR", "BANNANA"]

word = word_list [random.randrange(0,2)]

#Functions
def click_1 ():
if text in word == true:
print ("yay")
else:
print ("Boo")



#Frames
hangman_frame = Frame(root).grid(row=0, column=0)
letter_frame = Frame(root).grid(row=1, column=0)

#Buttons
A = Button(letter_frame, text="A", command=click_1).grid(row=0, column=0, sticky=W)
B = Button(letter_frame, text="B", command=click_1).grid(row=0, column=1, sticky=W)
C = Button(letter_frame, text="C", command=click_1).grid(row=0, column=2, sticky=W)
D = Button(letter_frame, text="D", command=click_1).grid(row=0, column=3, sticky=W)
E = Button(letter_frame, text="E", command=click_1).grid(row=0, column=4, sticky=W)
F = Button(letter_frame, text="F", command=click_1).grid(row=0, column=5, sticky=W)
G = Button(letter_frame, text="G", command=click_1).grid(row=0, column=6, sticky=W)
H = Button(letter_frame, text="H", command=click_1).grid(row=0, column=7, sticky=W)
I = Button(letter_frame, text="I", command=click_1).grid(row=0, column=8, sticky=W)
J = Button(letter_frame, text="J", command=click_1).grid(row=0, column=9, sticky=W)
K = Button(letter_frame, text="K", command=click_1).grid(row=0, column=10, sticky=W)
L = Button(letter_frame, text="L", command=click_1).grid(row=0, column=11, sticky=W)
M = Button(letter_frame, text="M", command=click_1).grid(row=0, column=12, sticky=W)
N = Button(letter_frame, text="N", command=click_1).grid(row=1, column=0, sticky=W)
O = Button(letter_frame, text="O", command=click_1).grid(row=1, column=1, sticky=W)
P = Button(letter_frame, text="P", command=click_1).grid(row=1, column=2, sticky=W)
Q = Button(letter_frame, text="Q", command=click_1).grid(row=1, column=3, sticky=W)
R = Button(letter_frame, text="R", command=click_1).grid(row=1, column=4, sticky=W)
S = Button(letter_frame, text="S", command=click_1).grid(row=1, column=5, sticky=W)
T = Button(letter_frame, text="T", command=click_1).grid(row=1, column=6, sticky=W)
U = Button(letter_frame, text="U", command=click_1).grid(row=1, column=7, sticky=W)
V = Button(letter_frame, text="V", command=click_1).grid(row=1, column=8, sticky=W)
W = Button(letter_frame, text="W", command=click_1).grid(row=1, column=9, sticky=W)
X = Button(letter_frame, text="X", command=click_1).grid(row=1, column=10, sticky=W)
Y = Button(letter_frame, text="Y", command=click_1).grid(row=1, column=11, sticky=W)
Z = Button(letter_frame, text="Z", command=click_1).grid(row=1, column=12, sticky=W)

Aucun commentaire:

Enregistrer un commentaire