From 7bd154691eee40c5496264016c56c6af8da68e4f Mon Sep 17 00:00:00 2001 From: User Date: Thu, 1 May 2025 16:31:59 +0300 Subject: [PATCH] Refracting. Instead of the basic while loop, a for loop --- main.py | 82 ++++++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/main.py b/main.py index 9c9ff2e..459e802 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,9 @@ import random - max_nam = 10 guess_num = 3 - def create_secret_num(guess_num): - """Создание случайного числа""" digits = list('0123456789') random.shuffle(digits) return ''.join(digits[:guess_num]) @@ -15,53 +12,42 @@ def attempt_check(attemt): clue = [] for i in range(len(attemt)): - if secret_num[i] == attemt[i]: + if attemt[i] == attemt[i]: clue.append("Fermi") - if attemt[i] in secret_num: + if attemt[i] in attemt: clue.append("Pico") - return ''.join(clue) if clue else 'Bagle' + return ' '.join(clue) if clue else 'Bagle' + +def game(): + secret_num = create_secret_num(guess_num) + print("I meade a number.") + attemt_num = 1 + + while max_nam >= attemt_num: + + attemt = '' + while len(attemt) != guess_num or not attemt.isdecimal(): + print(f"Attemt № {attemt_num}") # Вывод информации о том, сколько попыток сделал игрок + attemt = input("> ") + + if secret_num == attemt: + print("You win!") + break + + print(attempt_check(attemt)) # А вот и подсказка + + attemt_num += 1 + print("You're done trying") + print(f"The hiden numbers is {secret_num}") - -for attempt_num in range(1, max_nam + 1): - """Основной цикл для прекращения или повторения игры""" - secret_num = create_secret_num(guess_num) # Создание случайного числа - print("I meade a number.") # По сути, не нужное оповещение о создание числа(Создано для игрока) - attemt_num = 1 # Номер попытки игрока - - while max_nam >= attemt_num: - """Цикл, за счет которого осуществляются попытки""" - attemt = '' # Попытка игрока - - while len(attemt) != guess_num or not attemt.isdecimal(): - """Цикл для ввода попытки. Его можно было бы реализовать через if-elif, - но тогда у игрока была бы одна попытка.""" - print(f"Attemt № {attemt_num}") # Вывод информации о том, сколько попыток сделал игрок - attemt = input("> ") - - """Часть игры, где происходит проверка введенного игроком числа с загаданным(секретным) числом""" - - if secret_num == attemt: - """Проверка, полностью ли совпало число. - Её нельзя впихнуть в функцию attempt_check(), так как здесь есть ""break""" - print("You win!") - break - - print(attempt_check(attemt)) # А вот и подсказка - - attemt_num += 1 - - if max_nam < attemt_num: - # Финишная прямая. Говорим комплементы, называем секретное число и спрашиваем, хочет ли игрок продолжить - print("You're done trying") # У тебя закончились попытки - print(f"The hiden numbers is {secret_num}.You want repeat the game? 'yes' or 'no'") - answer = input("> ") +def main(): + while True: + game() + answer = input("You want repeat the game?> ").lower() if answer == 'no': - print("Thanks for playing") - break - else: - print("You want repeat the game? 'yes' or 'no'") - answer = input("> ") - if answer == 'no': - print("Thanks for playing") - break \ No newline at end of file + print("Thanks for playing") + break + +if __name__ == '__main__': + main() \ No newline at end of file