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! 😊 


Python Beginner: While Loop

 Hello, developer!


Have you made your own dictionary for your abbreviation?

Today, we will learn the "While Loop". "While Loop" enables you to repeat execution of some codes while the condition is "True". While loop executes indented codes repeatedly until condition result is "False". 


I shows "While Loop" declaration and form as below. 

While Loop declaration 

While condition:

        execution codes # Indented codes will be repeated until condition result is "False"


Following exercise program is a good example in order to understand how works "While loop" in Python. I showed all exercise codes and execution result as following.

i = 1
while i<=10: # Check the condition result is "True"
print(i)
i += 1 # Add 1 to variable "i" and input calculated value into variable"i"
# Back to while line and check the condition
print("Done with loop")

Can you see the same result? Great job! Now you can use "While loop" in the Python program. 

Let's see on next post, developer! 😊

Thursday, August 27, 2020

Python Beginner: Dictionaries

 Hello, developer!


Have you tried to upgrade your calculator? I encourage you modify codes and execute more codes as possible as you can. Then you can learn it more clearly and make better codes.


Today, we will learn the "Dicitionaries". "Dictionary" feature enables you can use predefined matched values in dictionary.  


Here is declaration of dictionary feature and an example.

Dictionary declaration and form

dictionery_name = { "value_1": "matched_value_1", "value_2": "matched_value_2", "value_3": "matched_value_3",...}

dictionery_name = { value_1: "matched_value_1", value_2: "matched_value_2", value_3: "matched_value_3",...} # value_1, value_2, ... can be numbers like 0, 1, 2,...

Example of dictionary

monthConversions = { "Jan":"January", "Feb":"February", "Mar": "March"}


You can call matched value by calling dictionary by below form.

Dictionary calling form

dictionary_name.get("value")


If there is no matched value in dictionary, it returns "None" message instead error message.

If you want to setting up the default none-matched value message, please use below form.

Setup of default none-matched return message

dictionary_name.get("value", "Default none-matched return message")


I showed full exercise codes and execution result as following.

monthConversions = {
"Jan":"January",
"Feb":"February",
"Mar": "March",
"Apr": "April",
"May": "May",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December"
}
print(monthConversions.get("Jul", "Not a valid key!"))

monthConversions2 = {
0:"January",
1:"February",
2: "March",
3: "April",
4: "May",
5: "June",
6: "July",
7: "August",
8: "September",
9: "October",
10: "November",
11: "December"
}
print(monthConversions2.get(12, "Not a valid key!"))

Can you see the same result? Great job! Now you can use "Dictionaries" feature in the Python. It will be helpful while you get abbreviations or acronyms input from users.

Alright, see you on next post, developer! 😊

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