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.
Читать →