I started programming in python recently and I am making a game of chance in which the computer randomly chooses a number between 1 and 10 and the player must guess the number. I realized this by generating a window with Tkinter. However, by launching the program, no matter what value I come in, even letters, it tells me that the number is greater. The error is in response() but I can not solve it. If you can help me and also tell me if you see any other errors I thank you very much! Here is my script:
from tkinter import *
from random import *
window = Tk()
window.geometry("400x400")
window.minsize(600, 500)
window.maxsize(600, 500)
window.title("Jeu de hasard")
window["bg"] = "grey"
def starting():
text_1 = Label(window, text="Choisis un nombre entre 1 et 10.", font=("Verdana", 15, "italic bold"), bg="grey", fg="white")
text_1.place(x=10, y=100)
def reponse():
nb = randint(1, 10)
player_reponse = champs_de_saisie.get()
player_reponse = int()
duration = 3000
if player_reponse < nb:
loose_text_1 = Label(window, text="Dommage, réessaie, le nombre est plus grand.", font= ("Verdana", 15, "italic bold"), bg="grey", fg="white")
loose_text_1.place(x=10, y=130)
loose_text_1.after(duration, loose_text_1.destroy)
if player_reponse > nb:
loose_text_2 = Label(window, text="Dommage, réessaie, le nombre est plus petit.", font=("Verdana", 15, "italic bold"),bg="grey", fg="white")
loose_text_2.place(x=10, y=130)
loose_text_2.after(duration, loose_text_2.destroy)
if player_reponse == nb:
win_text = Label(window, text="Bravo, tu as gagné !", font=("Verdana", 15, "italic bold"),bg="grey", fg="white")
win_text.place(x=10, y=130)
win_text.after(duration, win_text.destroy)
champs_de_saisie = Entry(window, bg="white", font="Arial 12", width=15)
champs_de_saisie.place(x=400, y=245)
champs_de_saisie.delete(0, END)
lbl = Label(window, text="Saisis ici :", font="Arial 12", bg="grey", fg="white")
lbl.place(x=430, y=215)
start_button = Button(window, text="Commencer", bg="blue", fg="white", font="Arial 15", width=27, command=starting)
start_button.place(x=0, y=0)
valid_button = Button(window, text="Valider", bg="green", command=reponse)
valid_button.place(x=445, y=275)
leave_button = Button(window, text="Quitter", bg="red", fg="white", font="Arial 15", width=27, command=window.destroy)
leave_button.place(x=300, y=0)
window.mainloop()
player_reponse = champs_de_saisie.get()
player_reponse = int()
Here you're setting player_response to a new int object, which gives you a 0.
You actually need to do this to cast an object to an int:
player_reponse = int(champs_de_saisie.get())
But ideally you'd validate that your data is actually a number first, otherwise you'll get an exception.