Архив рубрики: Публикации

Python: Problems and Solutions (Chapter 3. Branching, while Loops, and Pseudocode. Guess the Number Game).

We continue to learn programming. After the third chapter in the book: Michael Dawson “We Program in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I studied the features of working with text in the Python programming language, tasks were proposed. Let’s do them together. I will give my solution, and you write your options in the comments.

Program “Guess the number” from Igroglaz without looking at the solution:

import random



guess = ""

guess_num = 0

number = int(random.randint(1,100))



print ("I made a guess: number 1-100. Can you guess it?n")



while guess != number:

    guess = int(input("Enter your proposal: n"))

    if guess > number:

        print("No, it's smaller..n")

    elif guess < number:

        print("No, it's bigger..n")

    else:

        print("Finally, you got it!n")

    guess_num += 1



print ("Number was ", number, ". You guessed it with", 

guess_num, "times. Good job!")

    

input()


By the way, there is a bug in this program in the book. Outside the loop there is tries = 1; whereas it should start from zero.

1) Write a program – a simulator of a “surprise” pie – that would display one of five different “Surprises” at startup, chosen at random.

Читать

Python: Problems and Solutions (Chapter 4. For Loops, Strings, and Tuples. The Anagram Game).

We continue to practice programming. After the fourth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to use the for statement and create tuples, tasks were suggested. Let’s do them together. I will give my solution, and you write your options in the comments.

1. Write a “Counting” program that would count at the request of the user. We should allow the user to enter the beginning and end of the count, as well as the interval between called integers. Читать

Python: Problems and Solutions (Chapter 5 Lists and Dictionaries Hangman Game).

We continue to practice programming. After the fifth chapter in the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where I learned how to make dictionaries and use lists, it’s time to move on to practice. Let’s do our homework together!

Task: Write a program that will display a list of words in random order. All words from the presented list should be printed on the screen without repetition.

Читать

Python – loops (for, while, break, continue)

Loops in Python are pieces of code that repeat multiple times.

range(start,stop,step) – this is how the forLoop loop looks in general, where start and stop describe the actual beginning and end of the loop, including the start point, but not including the end point; step – the step with which the computer moves from the start point to the end point (another step is called increment).

Example:

range(1,10,2) means 1, 3, 5, 7, 9

Читать

Python: подготовка к работе с БД MySQL

Я часто “переезжаю” от одного окмпа на другой и приходится с нуля настраивать энвайромент для работы с MySQL. В итоге я каждый раз смотрю видос Штукенции на эту тему (он будет внизу этой статьи), что не очень удобно, когда ты в сотый раз это делаешь. Решил сделать шпаргалку по Питону 🙂

Итак, допустим, у нас есть скрипт на Питон.

  1. Первым делом – устанавливаем сам Питон.
  2. Далее надо, чтобы скрипт открывался в IDLE. Для этого:

    – ПКМ на файл -> Open with… -> More apps… -> Look for another app… -> C:Python310Libidlelibidle.bat
  3. Также зайдем сразу в настройки IDLE и выберем там тёмную тему: Options → Configure IDLE → Highlights → жмяк на кнопку IDLE Classic → выбираем IDLE Dark
  4. Теперь обновляем Пип в CMD: py -m pip install --upgrade pip
  5. Ставим MySQL коннектор: pip install mysql-connector-python
  6. Наслаждаемся и пишем коммент к этомук посту 😉 А вот и видео с наглядным гайдом:

Читать

Python: нестандартные функции. Игра “Крестики-нолики”.

Продолжаем учить язык программирования пайтон Python. Переходим к изучению 6 главы “Функции. Игра Крестики-нолики” по книге: Майкл Доусон “Программируем на Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), чтобы создавать собственные функции и работать с глобальными переменными.

Как создать функцию?

Общий вид функции в Python: название_функции()

Чтобы создать собственную функцию, нужно ее объявить.

Общий вид объявления функции

def название_функции(параметр1, параметр2...):

    '''Документирующая строка'''

    Блок выражений функции

Читать