Currently studying Python for class, I am creating a number guessing game.
objectives of code:
- a user rolls a dice, that determines how many attempts they get to guess a number between 1 to 100
- 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.