Mundo em Python

Adivinha o Número

from tkinter import *
import random
tentativas_restantes = 10
numero_secreto = random.randint(1, 100)
root = Tk()
root.geometry("400x250")
root.resizable(0, 0)
root.config(bg="#145787")
root.title("Adivinha o Número") titulo = Label(text="Adivinha o Número",
font=("Arial", "30", "bold"),bg="#145787",fg="#14e0c2")
titulo.place(relx=0.05, rely=0.05)
texto_sub1 = Label(text="Aposta:",
font=("Arial", "15", "bold"),bg="#145787",fg="#14e0c2")
texto_sub1.place(relx=0.25, rely=0.35) texto_sub2 = Label(text="De 1 a 100",
font=("Arial", "12", "bold"),bg="#145787",fg="black")
texto_sub2.place(relx=0.45, rely=0.45)
aposta = IntVar()
aposta_entrada = Entry(textvariable=aposta,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
aposta_entrada.place(relx=0.55, rely=0.35, relwidth=0.25) tentativas_label = Label(root, text=f"Tentativas restantes: {tentativas_restantes}",
font=("Arial", 12, "bold"), bg="#145787", fg="#14e0c2")
tentativas_label.place(relx=0.05, rely=0.7, relwidth=0.9)
def limpar():
aposta_entrada.delete(0, END)
resultado.set("") def adivinha_numero():
global numero_secreto, tentativas_restantes if tentativas_restantes > 0:
a = aposta.get()
if a == numero_secreto:
mensagem = "Acertou no número"
resultado_texto.config(fg="green")
else:
if a < numero_secreto:
mensagem = "Número menor que o número secreto."
else:
mensagem = "Número maior que o número secreto."

resultado_texto.config(fg="red")
tentativas_restantes -= 1

if tentativas_restantes == 0:
mensagem = f"Você perdeu! O número secreto era {numero_secreto}."
resultado_texto.config(fg="red")
but_novo_jogo.config(state=DISABLED) resultado.set(mensagem)
tentativas_label.config(text=f"Tentativas restantes: {tentativas_restantes}") def novo():
global numero_secreto, tentativas_restantes numero_secreto = random.randint(1, 100)
tentativas_restantes = 10
tentativas_label.config(text=f"Tentativas restantes: {tentativas_restantes}")
resultado.set("")
aposta_entrada.delete(0, END)
resultado_texto.config(fg="black")
but_novo_jogo.config(state=NORMAL)
but1 = Button(text="Verificar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=adivinha_numero)
but1.place(relx=0.05, rely=0.55, relwidth=0.25, relheight=0.1)
but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.35, rely=0.55, relwidth=0.25, relheight=0.1) but_novo_jogo = Button(text="Novo Jogo", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=novo)
but_novo_jogo.place(relx=0.65, rely=0.55, relwidth=0.25, relheight=0.1) resultado = StringVar()
resultado_texto = Label(textvariable=resultado,
font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9,relheight=0.15) root.mainloop()
0