Skip to main content

My Toolkit

Every developer needs a nice set of tools to help him to be more productive and focused towards his work. It may not be very easy for everyone to find a good set of tools. In this post, I am listing the tools that I have used and which may be very useful for you. IDEs IDEs are one of the most important tools that every developer needs. Some of the awesome IDEs that I have used and would recommend you are- 1) Adobe Brackets Brackets is a very lightweight IDE with many extensions giving it superpowers. If you are working on a small scale project and need speed, then Brackets is for you. It has awesome support for HTML, CSS and JS including smart autocomplete suggestions. Support for more languages can be extended via numerous extensions. Smart live preview gives more to programmers as they can see the changes as they code. This IDE is absolutely free of cost and available for downloading at  http://brackets.io/  . 2) VS Code Visual Studio Code gives the power of Microsoft's Visual S

A coin toss program in Python and JS

Today, we will increase our confidence in programming by making a coin toss program in two popular languages : Python and JavaScript.

Starting from Python:

Python is a very popular and readable language. Python developers have a large demand in the industry. So keeping this in mind, we will start with python. First of all, we will make an algorithm on which we will work.

1)Make two choices.
2)Have random computer input and human input.
3)Check if both inputs are same.
4)Print the result of the toss.

These are the basic steps. We will add additional features afterwards. So let's go...

1)Importing packages
Firstly, we will import a package which will help us to generate random computer choice.

import random
2)Creating choices
For creating two choices to select, we will make a list as follows-

options = ['heads', 'tails']

3)Selection
We can get user's choice using input()-

userchoice = input("Enter your choice - heads/tails ->")
For making random computer choice, we will use the random package that we imported at the starting.

compchoice = random.choice(options)

4)Comparision
We will use an if-else condition to compare both choices and print the result-

if userchoice == compchoice:
	print("You won")
else:
	print("You lost")

So this is our code as of now-

import random

options = ['heads', 'tails']

userchoice = input("Enter your choice - heads/tails ->")
compchoice = random.choice(options)

if userchoice == compchoice:
	print("You won")
else:
	print("You lost")

Hit the run button below and test the code-
Wow! Congratulations if you won the toss. Can we go a little further? The current code allows us to toss the coin only once. What if we want multiple tosses? So let's loop it.
We are going to use a while loop for this purpose. 

import random
options = ['heads' 'tails']

while True:
	#Content goes here...

Wait!!! This loop is going to execute till infinity. We need a stopper. Here it is..

import random
options = ['heads' 'tails']
cont = "yes"

while cont == "yes":
    userchoice = input("Enter your choice - heads/tails ->")
    compchoice = random.choice(options)

    if userchoice == compchoice:
	    print("You won")
    else:
	    print("You lost")
    
    cont = input("Do you want to continue? yes/no > ")

And here is the final version of the project. Try your luck in the toss-


Moving to JavaScript:

JavaScript is a very popular language in web development. So let's build a JavaScript Version of our project. We are going to use the alert and prompt functions for input and output. So let's go.

1)Options:

Making options will be somewhat same like Python. We will define an array.

const options = ["heads", "tails"];

2)User input:

We will use prompt() to take input.

let userinput = prompt("Enter your choice - heads/tails");

3)Random choice:

Generating random choice will be a little bit different in JS. We will store a random number in a variable and use the number to choose an item from the array from its index.

let randomNumber = Math.floor(Math.random()*2); //Gives 1 or 2
let randomItem = options[randomNumber]; //Access item by index

4)Comparision:

Comparision will be done by if-else condition:

if (userinput === randomItem){
    alert("You won :)");
} else{
    alert("You lost :(");
}

5)Repeating:

We can use a do-while loop for iteration.

let cont = "yes";
do{
    //Code
    //inside
    //the
    //loop
    cont = prompt("Do you want to continue? yes/no");
} while(cont === "yes");

So here is our complete code -

const options = ["heads", "tails"];
let cont = "yes";

do{
    let userinput = prompt("Enter your choice - heads/tails");
    let randomNumber = Math.floor(Math.random()*2);
    let randomItem = options[randomNumber];
    
    if (userinput === randomItem){
    	alert("You won :)");
    } else{
    	alert("You lost :(");
    }
    cont = prompt("Do you want to continue? yes/no");

} while(cont === "yes");

You can test the code below-
It's all for now. I hope that you liked this short tutorial. I will be back with another post on another topic. Till then, Happy Coding!!

Comments

Popular posts from this blog

Let's get Sassy.

Have you ever tried Sass in your StyleSheets? If not, then you will start using it from today. Sass is a CSS Preprocessor. Although, I am not going to explain what is Sass etc, but I am going to share some of the most useful features of Sass which I use a lot. So without wasting a single minute let's get started. 1)Variables: We would have used variables in programming languages like JavaScript, Python, C etc. But we can use variables in Sass also. Variables in Sass are mostly used to store colors, font size, etc. When we are dealing with many colors like #ffc000  or #ffff00  etc and our Sty leSheets become very large it becomes very difficult for us to remember each color's hex code and we mess up. We can overcome this problem by using variables. We can save #ffff00 as $yellow and use the color by the variable name wherever we want. You can see this in action in the below embed -  See the Pen Sass Variables by PraneetDixit ( @PraneetDixit ) on CodePen 2)Ne

Playing with CSS Cards

New design trends are coming everyday. Card layout in CSS has been a very popular layout type in the recent times and many companies like Google are using card layout in their designs. We will try to create a card layout in this short tutorial. - Prerequisites : Basic knowledge of HTML and CSS. Flexbox will be our friend in layout. 1) Basic file structure - The basic project structure will go as below : Project Directory     |-index.html     |-style.css 2)Basic html - <!--index.html--> <!DOCTYPE html> <html> <head> <title>CSS Cards</title> <link rel="stylesheet" href="style.css"> <!--CSS link--> </head> <body> <div id="container"> <div class="card one"> Card 1 </div> <div class="card two"> Card 2 </div> <div class="card