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


Messages In This Thread
Unable to count the number of tries in guessing game. - by Frankduc - Mar-20-2022, 06:41 PM

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