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.
#1
Hello,

pyscript is crashing if count = 0 declare inside def check(guess):
otherwise i get local variable 'count' referenced before assignment

import tkinter
from random import randint
from tkinter import messagebox


low = 0
high = 10
rand = randint(low,high)
print(rand)
essai = 10
count = 0
def check(guess):
    while(guess!=rand):
        if guess < rand:
            txtMess.configure(text = 'Trop bas')

        elif guess > rand:
            txtMess1.configure(text = 'Trop haut')

        else:
            txtMess2.configure(text = f"Bravo! {guess} est le bon nombre")
        count = count + 1


    print('count',count)

tk = tkinter.Tk()
tk.title('Deviner le nombre')

label = tkinter.Label(tk, text = f"Devinez un nombre entre {low} et {high} inclusivement")
label.pack()

entry =  tkinter.Entry(tk)
entry.pack()

button = tkinter.Button(tk, text = 'Devine', command = lambda: check(int(entry.get())) )
button.pack()

txtMess = tkinter.Label(tk, text = "")
txtMess.pack()

txtMess1 = tkinter.Label(tk, text = "")
txtMess1.pack()

txtMess2 = tkinter.Label(tk, text = "")
txtMess2.pack()

tk.mainloop()
Any explaination for that?
BTW anyone know a good compiler easy to use and light to install?

Thank you
Reply
#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
#3
Your code reset the random number every tries.
So its impossible to find the number. If you type 5 its to high if you type 4 its too low Wink
The while loop is to calculate the number of tries.

But thank you for this part of the code: check.count = getattr(check, 'count', 0)
count = check.count + 1
check.count += 1

I just tried it and its working fine.
Reply
#4
Change
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
to this
def randnum():
    '''
        Function for getting and returning a random number.
        Default value is 0
    '''
    randnum.number = getattr(randnum, 'number', rnd.randint(0, 10))
    # random_number = rnd.randint(0, 10)
    return randnum.number
The number should remain the same. The script will have to be restarted to get a new number
Frankduc likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Yes its cool thank you!
Reply
#6
Updated code to clear entry field on button submit, rest count, and get new number on correct guess of number

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', rnd.randint(0, 10))
    # random_number = rnd.randint(0, 10)
    return randnum.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 = 0
        randnum.number = rnd.randint(0, 10)

    check.count += 1
    entry.delete(0, tk.END)


root = tk.Tk()
root['padx'] = 8
root['pady'] = 5

guess_label = tk.Label(root, text='Tries:')
guess_label.pack()
label = tk.Label(root, text='Type a number between 0 and 10')
label.pack()
entry = tk.Entry(root)
entry.pack()
btn = tk.Button(root, text='Submit')
btn['command'] = lambda: check(entry.get())
btn.pack()
entry.focus()

msg = tk.Label(root)
msg.pack()

root.mainloop()
Frankduc likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
I was not asking that much but thank you again.
Reply
#8
Sorry, I intend to go overboard sometimes. Smile
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,268 Oct-18-2022, 03:52 AM
Last Post: Yegor123
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Problem : Count the number of Duplicates NeedHelpPython 3 4,282 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Count number of occurrences of list items in list of tuples t4keheart 1 2,344 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,521 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Count number of values Kristenl2784 0 1,488 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,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,685 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,132 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,218 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