1

Currently studying Python for class, I am creating a number guessing game.

objectives of code:

  1. a user rolls a dice, that determines how many attempts they get to guess a number between 1 to 100
  2. results are saved in a notepad.

I am able to get the guessing game working in shell. However, i am trying to convert game to GUI.

CURRENT ISSUE

I have assigned a tk.Button to run function when pressed. However, I am receiving an error.

Code below;

import random
import tkinter as tk
import time
import sys
window = tk.Tk()
window.title("Shanes Number Guessing Game")
window.geometry("900x500")
diceResult = random.randrange(1,6)
print (diceResult)
tries= 0
correct = 0

   
logo = tk.PhotoImage(file="C:\Python-Tkinter pics\\numberguess.png")
photo1 = tk.Label(image=logo)
photo1.image = logo
photo1.pack()

#gui buttons and lables

enterGuessLabel = tk.Label(window, text="enter guess below")
enterGuess =  tk.Entry(window)
diceButton = tk.Button(window, text="roll dice", command=throwDice)
diceResultLabel = tk.Label(window, text="you rolled a: ")


#Number guess code


def throwDice():
    count = diceResult
    while not count == 0:
        input1 = int(input("guess a number 1,10 "))
        randNum1 = random.randrange(1,10)

    if (input1 == randNum1):
        print("correct")
        correct += 1
                
    else:
        print ("incorrect")
        tries += 1
        print (randNum1)
        print (count -1)
        count -= 1
        print ("computer", (tries) ,(userName),(correct))



#GUI pack

enterGuessLabel.pack()
enterGuess.pack()
diceButton.pack()
diceResultLabel.pack()
window.mainpack()

Here is the output

    5
name 'throwDice' is not defined
Stack trace:
 >  File "C:\Users\shane\source\repos\ICT30118 CertIII Assessment\ICT30118 CertIII Assessment\ICT30118_CertIII_Assessment.py", line 31, in <module>
 >    diceButton = tk.Button(window, text="roll dice", command=throwDice)
Loaded '__main__'
Loaded 'runpy'

I have assigned a "tk.Button" to run the command "throw dice" when pressed. I have assigned a "def function" as "throwDice():"

When I run the program, I receive an error that "throwDice" is not defined

Any help will be appreciated.

  • 1
    You need to define your function before you use it as a command. Usually you define your global function right after you imported your stuff. Keep in mind that your interpreter reads your code left to right and up to down. – Thingamabobs Aug 08 '20 at 10:31
  • Also take a look at this https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Thingamabobs Aug 08 '20 at 10:33
  • I am unsure of how to define a function with the same name, i have attempted to use the global function. will post results in main post. –  Aug 08 '20 at 10:39
  • I see I had confused you. What I mean by a global function is that your function is in the global namespace. You dont need to define that function twice, you need to place it at the right spot. Again your Intepreter reads your code up to down. Your function is on the very bottom of your code and your button is in the middle. How should your Interpreter know your funtion at this point? Link for global namespace https://medium.com/@gough.cory/understanding-python-namespaces-through-recursion-c921cbd4e522 – Thingamabobs Aug 08 '20 at 10:47
  • 1
    It worked, I will edit post, thanks. –  Aug 08 '20 at 10:54
  • Congratulations! :) – Thingamabobs Aug 08 '20 at 10:55

2 Answers2

2

As per the advise of Stack Overflow user "Atlas435" I have simply moved the tk.Button line of code under the "def function" code. The interpreter reads code from top to bottom, so for the tk.Button(command=xyz) to work the interpreter first needs to read the function.

Code below:

def throwDice():
    count = diceResult
    while not count == 0:
        input1 = int(input("guess a number 1,10 "))
        randNum1 = random.randrange(1,10)

    if (input1 == randNum1):
        print("correct")
        correct += 1
                
    else:
        print ("incorrect")
        tries += 1
        print (randNum1)
        print (count -1)
        count -= 1
        print ("computer", (tries) ,(userName),(correct))

diceButton = tk.Button(window, text="roll dice", command=throwDice)

#GUI 
enterGuessLabel = tk.Label(window, text="enter guess below")
enterGuess =  tk.Entry(window)
diceResultLabel = tk.Label(window, text="you rolled a: ")
enterGuessLabel.pack()
enterGuess.pack()
diceButton.pack()
diceResultLabel.pack()
0

Define the throwDice() function before defining the button

Because the python interpreter reads from the top