Python Forum
Unable to count the number of tries in guessing game.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to count the number of tries in guessing game.
#2
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.

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


Reply


Messages In This Thread
RE: Unable to count the number of tries in guessing game. - by menator01 - Mar-20-2022, 07:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,403 Oct-18-2022, 03:52 AM
Last Post: Yegor123
Question Beginner Boolean question [Guessing game] TKB 4 2,417 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Problem : Count the number of Duplicates NeedHelpPython 3 4,483 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Count number of occurrences of list items in list of tuples t4keheart 1 2,418 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,758 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Count number of values Kristenl2784 0 1,538 Jul-16-2020, 01:26 PM
Last Post: Kristenl2784
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,859 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,830 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,226 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,351 Mar-09-2020, 04:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020