Get Your Feet Wet, Now!
Tags: python, beginners
Published: 2023-02-03 11:52:48

Almost all programming languages welcoming their beginner users with "Hello, World!" coding. It's a simple program that will showing the result immediately to the user's screen. For the very newbie people, it surely feels magical how they can command their computer to show a sentence without using any text processing program. But if they are like me who are easily bored in learning something new, doing "Hello, World" each time I learning new language is not as magical as I think before. 

Enter the "guess the number" game. Any language who have random number function and can receive a user input can make it. It helps teach people about how the language interact with the data, how it receive user's input, and also--the most important thing--how to do conditional programming. 

Okay, I know someone (or many of you) will say, "Whoa! Hold your horses! How can you expect a newcomer, someone who don't understand programming at all, can handle that so much things in one go? It's better to just give them the "Hello, World" example. Nothing wrong with that?" Yes, you are right. Nothing wrong with that. It's just me, someone who needs some _oomph_ in learning another new language and don't mind to make my feet wet directly. The "Hello, World!" tradition made me boring already. I want to make something and learning how to programming by doing that, that's the reason I spend my time learning programming language this past year. That's why I need some simple program, not too complex, but also not too easy. 

I just decided to take Python seriously in November 2021. I read too much tutorials and doing a few program that I realize too much to digest for my very newbie brain that time. I made a blackjack program. It taught me about how to receive user's input, how to handle conditional iteration with for loop, how to use while loop, and also giving me a big headache because I got so many errors which I cannot handle properly that time. And not to mention my stupidity trapping myself in so many recursive steps. Then I thought, what if I made something simple first? I found a tutorial about how to make a guessing game and try it. And this is what I was made that time:

```python
import random

def guess_choice(random_guess, chance):
    ''' Compare guesses with randomized number created by the program .'''
    while chance > 0:
        print("-" * 8)
        your_guess = int(input("Enter your guess:  > "))         
        if your_guess == random_guess:
            print("You guess it right!")
            print("-" * 8)
            break
        elif your_guess < random_guess:
            print("Your guess is too low.")            
            chance -= 1
            print(f"You have {chance} life point(s) left.")
            print("-" * 8)
        elif your_guess > random_guess:
            print("Your guess is too high!")            
            chance -= 1
            print(f"You have {chance} life point(s) left.")
            print("-" * 8)
    return chance

while True:
    game_on = True
    guess = random.randint(0, 100)
    print("=" * 8)
    print("Let's guess a number between 0 to 100.")

    while game_on:
        difficulty = input("Choose your difficulty level. Press '1' to choose EASY LEVEL, or press '2' to choose HARD LEVEL:  > ")
        if difficulty == '1':
            print("=" * 8)
            print("You have 10 life points. Each wrong guess, you'll lose one life. ")
            life = guess_choice(guess, 10)
            game_on = False
        elif difficulty == '2':
            print("=" * 8)
            print("You have 6 life points. Each wrong guess, you'll lose one life. ")
            life = guess_choice(guess, 6)
            game_on = False
        else:
            print("Please only choose '1' or '2'.")
        
    if life > 0:
        print("=" * 8)
        go_again = input("Would you continue playing? Press 'y' to continue, or press any other keys to stop:  > ").lower()
        if go_again == 'y':
            continue
        else:
            print("Thank you for playing.")
            break
    else:
        print("-" * 8)
        print(f"The right number is {guess}.")
        print("Your life point is 0. You can't continue.")
        print("Thank you for playing.")
        print("=" * 8)
        break
```

... Yeah. The code is verbose and repeating. But this is me in April 2022. I learned how to control while loop, for loop, and handling user input. And also how to interact with the user. I remember the happiness when my program asking my choice. I remember how fun it was when I correctly guessing a random number generated by my computer. I feel like a real programmer that time.

I grow up since that day. I pick other topics in python. I tried data processing with pandas, done some web scraping with BeutifulSoup, making simple GUI with flet library. I know how to handle exceptions whenever I get them. I make my code DRY. And today I made the same program as above. By SLoC (Source Line of Code) this code is longer. But by structure it's more pythonic and clean:

```python
from random import randint

def guess_choice():
    '''Compare guesses with randomized number created by the program .'''
    print("=" * 10)
    print(
        '''
        Welcome to GUESS THE NUMBER game.
        In this game the computer will pick a number from 0 to 100.
        Your task is to guess the number.
        But before that, let's choose the difficulty level.
        '''
    )
    print("=" * 10)

    while True:
        difficulty = input(
            '''
            Choose only the number:
            1. Easy mode.
            2. Normal mode.
            3. Hard mode.

            '''
            )
        
        if difficulty.isdigit():
            if int(difficulty) > 3 or int(difficulty) < 1:
                print("Please select from 1 to 3 only.")
            else:
                if difficulty == '1':
                    chance = 10
                    print("You have 10 chances to guess the number.")
                    break
                elif difficulty == '2':
                    chance = 8
                    print("You have 8 chances to guess the number.")
                    break
                else:
                    chance = 5
                    print("You only have 5 chances to guess the number")
                    break
        else:
            print("Please enter only a number.")

    print("Good luck!")
    print("=" * 10)

    rng = randint(0, 100)

    while chance > 0 :
        chance -= 1 # Reduce chance by 1
        guess = int(input("Enter your guess: >  "))

        if guess > 100:
            print("Choose only in range 0 to 100.")
        elif guess < rng:
            print("Choose HIGHER number.")
        elif guess > rng:
            print("Choose LOWER number.")
        else:
            print("CONGRATULATION! YOU GET THE NUMBER!")
            chance += 1 # Adding back 1 chance soit will never be 0
            break

        print(f"You have {chance} chance(s) left.")
        print("=" * 10)
    
    if chance <= 0:
        print("You LOSE this game.")
        print("=" * 10)
    else:
        print("You WIN this game.")
        print("=" * 10)
            
        
if __name__ == '__main__':
    while True:
        guess_choice()
        restart = (input(
            '''
            PRESS Y KEY TO CONTINUE OR OTHER KEYS TO LOG OUT.
            '''
        )).lower()

        if restart == 'y':
            print("Let's play it again!")
            print("=" * 10)
            continue
        else:
            print("Thank you for playing the game.")
            print("=" * 10)
            break
```

With this approach, I make my very first program in Dart. This is the first time I learn this language. And by making a simple guessing number program I realize how different Dart and python handling the user input. I realize I can't directly type casting the user input to integer like when I use python because it might having a null value. I learn how to handle this using ternary operator to give the input a default value. I learn the similarity on string formatting between Dart and Python (f-string for python is similar with $ for Dart).

```dart
import 'dart:io';
import 'dart:math';

void main() {
  var rng = Random().nextInt(30);
  print('Choose a number between 0 to 30: ');
  var choice = stdin.readLineSync();
  int chosen_num = int.parse(choice ?? '0');
  print('I will choose a number too.');
  print("Let's compare! Mine is $rng. And yours is $choice");

  if (chosen_num > 30) {
    print("Hey! It's cheating!");
    print("You should choose a number only between 0 to 30!");
  } else if (rng > chosen_num) {
    print("Mine is bigger!");
    print("Haha! I beat you!");
  } else if (rng < chosen_num) {
    print("Aw, damn! Yours bigger!");
    print("I'm lost!");
  } else {
    print("Wow! It's a tie!");
  }
}

```

Get your feet wet sooner, folks. Make something simple that can spark your interest to learn the programming language. That way, you can adapted with the language faster. Happy coding! ;)