-->

Python 3.2.1.3 LAB: Essentials of the While Loop - Guess the Secret Number

Essentials of the While Loop - Guess the Secret Number

Guess the secret number
Guess the secret number

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.3 LAB: Essentials of the while loop - Guess the secret number

Scenario

A junior magician has picked a secret number. He has hidden it in a variable named secret_number. He wants everyone who run his program to play the Guess the secret number game, and guess what number he has picked for them. Those who don't guess the number will be stuck in an endless loop forever! Unfortunately, he does not know how to complete the code.

Your task is to help the magician complete the code in the editor in such a way so that the code:
will ask the user to enter an integer number;
will use a while loop;
will check whether the number entered by the user is the same as the number picked by the magician. If the number chosen by the user is different than the magician's secret number, the user should see the message "Ha ha! You're stuck in my loop!" and be prompted to enter a number again. If the number entered by the user matches the number picked by the magician, the number should be printed to the screen, and the magician should say the following words: "Well done, muggle! You are free now."

The magician is counting on you! Don't disappoint him.

Solution Code:

hint: I always highlight the important parts in the scenario that would help you as a future programmer to solve the mystery.

secret_number = 777
print(
"""
+=================================+
| Welcome to my game, muggle!     |
| Enter an integer number         |
| and guess what number I've      |
| picked for you.                 |
| So, what is the secret number ? |
+=================================+
""")

number = int(input("Enter an integer: "))

while number != secret_number: #If Condition equals to False
    print("Ha ha! You're stuck in my loop!¯\_( ͡👁️ ͜ʖ ͡👁️)_/¯") #1st while instruction
    number = int(input("Enter an integer: ")) #2nd while instruction (the loop)
else: # If the condition equals to True
    print(number , " is the secret number (>‿◠)✌\n",  #else instruction
    "Well done, muggle! You are free now. ❤💋")

=================================================================================
Follow the blog to be the first to know ❤

Post a Comment

0 Comments