Popular Posts

Friday, August 28, 2020

Building a Guessing Game

 Hello, developer!


Are you familiar to use "While loop"? I encourage you to use it as many as enough, so you understand it as well.


Today, we will build a "Guessing Game". In this game, we will set up a secret word and get input from user and compare secret word and input string. If two strings are same, the game print winning message and end the game. Otherwise, two strings are different and guess trying count is less than 3, program shows guessing input message and get input guessing string from user once again. If guess trying count reaches 3, it shows losing message and end it up.


We will use "While Loop" and "If Statements" for this game.

I showed full exercise guessing game codes and execution result as following.

secret_word = "Dinosaur"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = True

if out_of_guesses:
print("Out of Guesses, You lose!")
else:
print("You win!")

Can you see the same result in this program? Great job! Today, you applied "While Loop" an "If Statements & Comparisons" in the Python program.

I encourage you to modify this game for extending guessing trying count or, change output message and secret_word as you want.

Alright, see you on next post! 😊 


No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...