# 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.

Shtukensia’s variant:

```import random
surpriz_1='Unicorn'
surpriz_2='Medal'
surpriz_3='Funny cat'
surpriz_4='Palm tree'
surpriz_5='Dino'
rezult=random.randint(1,5)
if rezult==1:
print(surpriz_1)
elif rezult==2:
print(surpriz_2)
elif rezult==3:
print(surpriz_3)
elif rezult==4:
print(surpriz_4)
else:
print(surpriz_5)``` Igroglaz variant:

```import random

num = random.randint(1,5)

if num == 1:
pie = "селедкой"
if num == 2:
pie = "мясом"
if num == 3:
pie = "курицей"
if num == 4:
pie = "сеном"
if num == 5:
pie = "опилками"

print ("Держи пирожок с", pie)

input()```

2) Write a program that would “Toss” a conventional coin 100 times and tell you how many times it came up heads and how many times it came up tails.

Shtukensia’s variant:

```import random
orel_1='orel'
reshka_2='reshka'
orel_sum=0
reshka_sum=0
count=0
while count!=100:
count+=1
result=random.randint(1,2)
if result==1:
orel_sum+=1
else:
reshka_sum+=1
print('Орел:',orel_sum,'Решка:',reshka_sum)``` Igroglaz variant:

```import random

n = 0
counter = 0

while n < 100:
flip = random.randrange(2)
if flip == 0:
coin = "Орёл"
counter += 1
else:
coin = "Решка"
print ("Бросаем монетку..", coin)
n += 1

print ("Орёл выпал", counter, "раз; решка", 100-counter, "раз")

input()
```

3) Modify the “Guess the Number” program so that the player has a limited number of attempts. If the player does not fit into the given number (and loses), then the program should display as harsh text as possible.

Shtukensia’s variant:

```import random
print('Я загадала целое число от 0 до 100. Угадай с 5 попыток!')
chislo=random.randrange(101)
popitka_count=0
while popitka_count<=4:
popitka_count+=1
popitka=int(input('Это число:'))
if popitka<chislo:
print('Больше!')
elif popitka>chislo:
print('Меньше!')
else:
print('Ничего себе! Ты отгадал! Это правда',chislo)
print('Количество попыток:',popitka_count)
break
if popitka_count==5 and popitka!=chislo:
print('О, ужас! Ты совершенно не умеешь читать мои мысли!\n\
Так и не смог угадать число за 5 попыток :(')``` Igroglaz variant:

```import random

guess = ""
guess_num = 0
number = int(random.randint(1,100))

print ("Try to guess a number 1-100 with 6 tries..\n")

while guess != number:
if guess_num > 6:
print("Sorry, you've lost! It was", number)
break
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

if guess == number:
print ("Number was ", number, ". You guessed it with", \
guess_num, "times. Good job!")

input()```

4) But the task is more difficult. Write in pseudocode an algorithm for a game in which a random number from 1 to 100 is guessed by a person and guessed by a computer. Before embarking on a solution, think about what the optimal guessing strategy should be. If the pseudocode algorithm is successful, try implementing the game in Python.

Shtukensia’s variant:

```print('Загадай любое целое число от 1 до 100. \
А я его отгадаю. Говори мне </>. \
Когда я угадаю, напиши "yes"\n')
input('Нажми Entr, когда загадал')
import random
low_num=0
high_num=100
gues=random.randint(low_num,high_num)
print('Это число',gues,'?')
otvet=input('Угадал?\n')
while otvet!='y':
if otvet=='<':
high_num=gues-1
gues=random.randint(low_num,high_num)
print(gues)
otvet=input('Угадал?\n')
elif otvet=='>':
low_num=gues+1
gues=random.randint(low_num,high_num)
print(gues)
otvet=input('Угадал?\n')
input('Ура! Я отгадал! Нажми Enter, чтобы выйти.')``` Igroglaz variant: To solve such problems effectively, you need to draw graphs or diagrams. Draw a straight line and mark there 0 at the beginning and 100 at the end… the bisection method is to take the average value and assign it to either the beginning or the end of the graph in the new iteration):

```# Bisection method

begin = 0
end = 100
guess = ""

print ("I'll guess a number 1-100.. answer 'yes' if I correct \
and 'less' or 'more' if number is bigger or smaller\n")

while True:
guess = (begin + end) // 2
print ("Is it", int(guess), "?")