Learn Python Easy way - Example 1

Let do some math using Python. Suppose you have invited your friends to your Birthday. You have a box of chocolates. You know how many friends are there and how many chocolates are there inside your box. You need to give equal number of chocolates to each of your friends, and you can keep the remaining chocolates in your box.


Write a program in Python to calculate how many chocolates can be given to each of your friends and how many remain in the box. The number of friends and chocolates can be input by the user and display number of chocolates for each friend and the number of remaining chocolates.

So first we will ask how many friends are there at your Birthday, and how many chocolates you have with you.

print ("How many friends are there ?")
friends = int (input ())
print("How many chocolates have you got ?")
chocolates = int (input ())

We will first use integer division to calculate the maximum number of chocolates that can be distributed equally. We can use the // integer division symbol for that in Python

maxChocolates = chocolates // friends

now we will find out how many chocolates remain after distributing. We can use the % modulus operator for that.

remainingChocolates = chocolates % friends

Now display the results

print ("You can share " , maxChocolates , " chocolates to each of your friends")
print("And there will be " , remainingChocolates , " chocolates left in your box")

friends, chocolates, maxChocolates and remainingChocolates are all variables defined by us. We are using them as integer variables.  All of these ways of writing print statements can be useful in different circumstances.

We are using a comma ( , ) to separate or + to concatenate different strings and % to concatenate strings and integers. We can write the same print statements in three different ways.

print ("You can share", maxChocolates, "chocolates to each of your friends")

print ("You can share " + str(maxChocolates) + " chocolates to each of your friends")

print ("You can share %d chocolates to each of your friends" %maxChocolates)

To run the following program first click on edit on repl.it to open the compiler in full screen and after that click on run. Enter the user input in the black panel.


Next : Find your Numerology Life path from your date of birth