wrk 2025-01-31 15-25-58
AI ии test задача: приведи пример на питоне , для образовательных целей, который затрагивает все конструкции языка =================== https://www.perplexity.ai/ import random def guess_number(): number_to_guess = random.randint(1, 100) # Генерация случайного числа от 1 до 100 attempts = 0 guessed = False print("Я загадал число от 1 до 100. Попробуй угадать!") while not guessed: user_guess = input("Введите ваше число: ") # Проверка на корректность ввода if not user_guess.isdigit(): print("Пожалуйста, введите целое число.") continue user_guess = int(user_guess) attempts += 1 # Условные конструкции для проверки угаданного числа if user_guess < number_to_guess: print("Слишком мало! Попробуйте снова.") elif user_guess > number_to_guess: print("Слишком много! Попробуйте снова.") else: guessed = True print(f"Поздравляю! Вы угадали число {number_to_guess} за {attempts} попыток.") # Запуск игры guess_number() ======== локальный Qwen-14B-Q6_K.gguf ===================== # Example Python Program Demonstrating Various Language Constructs def greet(name): """Function to greet someone by name.""" print(f"Hello, {name}!") def add_numbers(a, b): """Add two numbers and return the result.""" return a + b if __name__ == "__main__": # Conditional statements x = 10 if x > 5: print("x is greater than 5") elif x == 5: print("x equals 5") else: print("x is less than 5") # Loops for i in range(5): print(f"Looping: {i}") while i < 10: print(f"While Loop: {i}") i += 1 # Exception Handling try: number = int(input("Enter a number: ")) print(f"You entered {number}") except ValueError: print("Please enter a valid integer") # Recursion def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) print(f"Factorial of 5: {factorial(5)}") # Lambda Function multiply = lambda a, b: a * b print(f"Lambda function result: {multiply(3,4)}") # Generator def simple_generator(): yield 1 yield 2 yield 3 gen = simple_generator() for num in gen: print(f"Generator value: {num}") # Class and Object-Oriented Programming class Dog: def __init__(self, name): self.name = name def bark(self): return f"{self.name} says woof!" my_dog = Dog("Buddy") print(f"My dog's name: {my_dog.name}") print(my_dog.bark()) # File Operations with open("example.txt", "w+") as file: file.write("Writing to a filenSecond linenThird line") with open("example.txt", "r") as file: lines = file.readlines() for line in lines: print(f"File content: {line.strip()}") # Boolean Operations a, b = True, False print(f"a AND b: {a and b}") print(f"a OR b: {a or b}") print(f"NOT a: {not a}") # Mathematical Expressions x = 10 y = 5 print(f"x + y: {x + y}") print(f"x ** y: {x ** y}") print(f"x % y: {x % y}") # Data Structures list_example = [i for i in range(5) if i % 2 == 0] print(f"List Comprehension: {list_example}") dictionary = {'a': 1, 'b': 2} print(f"Dictionary values: {dictionary.values()}") my_set = set([1, 2, 3, 4]) another_set = set([3, 4, 5]) print(f"Set union: {my_set.union(another_set)}") print(f"Set intersection: {my_set.intersection(another_set)}") my_tuple = (1, 'a', True) print(f"Tuple elements: {my_tuple}") # Module Usage import os print(f"Current working directory: {os.getcwd()}")