Posts: 201
Threads: 37
Joined: Dec 2021
Mar-20-2022, 06:41 PM
(This post was last modified: Mar-20-2022, 06:41 PM by Frankduc.)
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
Posts: 1,144
Threads: 114
Joined: Sep 2019
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()
Posts: 201
Threads: 37
Joined: Dec 2021
Mar-20-2022, 07:58 PM
(This post was last modified: Mar-20-2022, 08:03 PM by Frankduc.)
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
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.
Posts: 1,144
Threads: 114
Joined: Sep 2019
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
Posts: 201
Threads: 37
Joined: Dec 2021
Posts: 1,144
Threads: 114
Joined: Sep 2019
Mar-20-2022, 08:11 PM
(This post was last modified: Mar-20-2022, 08:11 PM by menator01.)
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()
Posts: 201
Threads: 37
Joined: Dec 2021
I was not asking that much but thank you again.
Posts: 1,144
Threads: 114
Joined: Sep 2019
Sorry, I intend to go overboard sometimes.
|