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

Python – показать пользователю результат

Когда мы пишем любую программу на любом языке программирования, то раньше или позже мы хотим, что результат нашего программирования показывался пользователю. Для этого мы используем команду:

print(…) обязательно с маленькой буквы Читать

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: non-standard functions. Game “Tic-tac-toe”.

We continue to learn the Python programming language Python. Let’s move on to chapter 6 “Functions. Tic-Tac-Toe Game” from the book: Michael Dawson “Python Programming”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition) to create our own functions and work with global variables .

How to create a function?

General view of a function in Python: function_name()

To create your own function, you need to declare it.

General form of a function declaration

def function_name(parameter1, parameter2...):

    '''Document string'''

  Function expression block

Читать

Python: Lists and Dictionaries

We continue to learn the Python programming language Python. We turn to the study of chapter 5 of the book: Michael Dawson “Programming in Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition), where we will master lists, list methods and dictionaries.

Lists in Python

Lists are similar to tuples, which we studied in the last chapter. It’s important to remember that, unlike tuples, lists can change. The general principles for working with tuples apply to lists.

Читать