
From tkinter import * from random import * root = Tk() root.title('Вгадай число!')
root.geometry('320x200') n=randint(1,100) i=0 def f(): text.delete(0.0,END) k=int(entry.get()) if n==k: text.insert(1.0, '\nЦе число %s.' % str(n)) else: text.insert(1.0,"Спроба № %s. Не вгадав" %i) i+=1 if k>n: text.insert(2.0,", багато") if k text.insert(2.0,", мало") lab1 = Label(root, text='Я загадав число від 1 до 100', font='Arial 15') lab1.grid(row=0, column=0, columnspan=4, sticky='w') lab2 = Label(root, text="Введи відповідь: ", font="Arial 15") lab2.grid(row=1, column=0, sticky='w') entry = Entry(root, width=10) entry.grid(row=1, column=2, sticky='w') button = Button(root, text='Натисни', font="Arial 15", bg='lightpink',command=f) button.grid(row=2, column=0, sticky='w') text = Text(root, width=40,height=10, bg='lightpink') text.grid(row=3, column=0, columnspan=4,sticky='w') root.mainloop() Доробити програму "Вгадай число" Програма повинна рахувати кількість спроб та робити підказку користувачеві, виводячи повідомлення "число менше", чи "число більше"; коли число відгадане, вказати кількість спроб, за яку це вдалося зробити гравцю.

Ответы на вопрос

Ответ:
from tkinter import *
from random import randint
root = Tk()
root.title('Вгадай число!')
root.geometry("320x200")
n = randint(1, 100)
i = 0
def f():
global i
text.delete(0.0, END)
k = int(entry.get())
if n == k:
i += 1
text.insert(1.0, '\nТи вгадав число %s за %s спроб' % (str(n), str(i)))
button['state'] = 'disabled'
else:
i += 1
if k > n:
text.insert(1.0, "Cпроба N° %s. Число %s, введене число більше." % (i, str(k)))
else:
text.insert(1.0, "Cпроба N° %s. Число %s, введене число менше." % (i, str(k)))
lab1 = Label(root, text='Я задав число від 1 до 100', font='Arial 15')
lab1.grid(row=0, column=0, columnspan=4, sticky='w')
entry = Entry(root, width=10)
lab2 = Label(root, text="Введи відповідь: ")
lab2.grid(row=1, column=0, sticky='w')
entry.grid(row=1, column=2, sticky='w')
button = Button(root, text='Натисни', font="Arial 15", bg='lightpink', command=f)
button.grid(row=2, column=0, sticky='w')
text = Text(root, width=40, height=10, bg='lightpink')
text.grid(row=3, column=0, columnspan=4, sticky='w')
root.mainloop()



To improve the "Guess the Number" program, you can add functionality to count the number of attempts and provide hints to the user. Here's an updated version of the program:
```python from tkinter import * from random import *
root = Tk() root.title('Вгадай число!') root.geometry('320x200')
n = randint(1, 100) i = 0
def f(): text.delete(0.0, END) k = int(entry.get()) if n == k: text.insert(1.0, '\nЦе число %s.' % str(n)) text.insert(2.0, 'Вам знадобилося %s спроб(и).' % str(i)) else: text.insert(1.0, "Спроба № %s. Не вгадав" % i) i += 1 if k > n: text.insert(2.0, ", багато") else: text.insert(2.0, ", мало")
lab1 = Label(root, text='Я загадав число від 1 до 100', font='Arial 15') lab1.grid(row=0, column=0, columnspan=4, sticky='w')
lab2 = Label(root, text="Введи відповідь:", font="Arial 15") lab2.grid(row=1, column=0, sticky='w')
entry = Entry(root, width=10) entry.grid(row=1, column=2, sticky='w')
button = Button(root, text='Натисни', font="Arial 15", bg='lightpink', command=f) button.grid(row=2, column=0, sticky='w')
text = Text(root, width=40, height=10, bg='lightpink') text.grid(row=3, column=0, columnspan=4, sticky='w')
root.mainloop() ```
In this updated version, we added a counter variable `i` to keep track of the number of attempts. Each time the user makes a guess, the program checks if the guess is correct. If it is, the program displays the number and the number of attempts it took. If the guess is incorrect, the program increments the counter and provides a hint to the user whether the number is too high or too low.
Please note that this is just one possible way to improve the program. There are many other ways to enhance it further, such as adding a limit to the number of attempts or implementing a scoring system. Feel free to customize and expand upon this code to suit your needs.


Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili