Mar-20-2022, 07:47 PM
I do not understand the while loop inside the check function.
Crashing probably because the count is not global inside the function.
Anyway I done a version of your game.
Crashing probably because the count is not global inside the function.
Anyway I done a version of your game.
import tkinter as tk from tkinter import messagebox as mb import random as rnd def randnum(): ''' Function for getting and returning a random number. Default value is 0 ''' randnum.number = getattr(randnum, 'number', 0) random_number = rnd.randint(0, 10) return random_number def check(guess): ''' Function for checking guessed number against random number. Increases count for guessing. Sets count default 0 Updates labels accordingly ''' check.count = getattr(check, 'count', 0) count = check.count + 1 guess_label['text'] = f'Tries: {count}' random_number = randnum() if int(guess) > random_number: msg['text'] = 'Too High' elif int(guess) < random_number: msg['text'] = 'Too Low' else: msg['text'] = f'Great! You guessed {random_number}' check.count += 1 root = tk.Tk() root['padx'] = 8 root['pady'] = 5 num = tk.IntVar() guess_label = tk.Label(root, text='Tries:') guess_label.pack() label = tk.Label(root, text='Type a number') label.pack() entry = tk.Entry(root, textvariable=num) entry.pack() btn = tk.Button(root, text='Submit') btn['command'] = lambda: check(entry.get()) btn.pack() msg = tk.Label(root) msg.pack() root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags