miércoles, 18 de enero de 2017

Fun with Numbers

FUN WITH NUMBERS

For our first project in python 3 we were asked to create a not so sophisticated code that asks for two numbers and automatically gives the sum, difference, product, division, and modulus operator of both numbers.

I´ll give a small explanation on what I did to make this code work and also I´ll share the sources where I found very useful information.

1.-First you must create a new project on the text editor, name it as you wish and remember to type ".py" at the end of the file´s name.



2.- Now that you have opened your text editor, start by declaring variables and making the code ask the numbers the user wants to use. For this, first give the variable a value which has to have the input function so that it lets you type a number o anything in the computer´s terminal.(you can find some more info on how to use the input function in the interactive book "How to think like a computer scientist", it explains every detail of the function in chapter 2.)

3.- It is important that if you want to use decimal numbers, you must use the float function in the next line of your code, just like this:
x= input ("Write a number")
x=float (x)

4.- Now that you´ve done the same thing for the "y" variable, you must declare a third variable which will be related to the answer to de x+y operation. Also remember that if your´re using decimal numbers and you want an exact result of the sum you must keep using the float function.
n= (x + y)
n= float (n)

5.- The 5th step will be to show the result or in other words, print it on the computer´s terminal. This has to be done with the print function. The syntax is very simple and looks like this: 
print ("The sum of both numbers is", n)    

remember that if you want to type a message before the result shows up, you must use quotations only in the text you type, this means, the quotations don't involve the variable.
6.- For the next operations, follow the same steps and try to give the code some order by leaving some space between lines using print("").


At the end your code should look something like this.

x= input ("Write a number")
x=float (x)
y= input ("Write a number")
y=float (y)
n= (x + y)
n= float (n)
print ("The sum of both numbers is", n)
print("")
print ("")
n2=(x - y)
n2= float (n2)
print ("The difference of both numbers is", n2)
print("")
print("")
n3=(x*y)
n3=float(n3)
print ("The product of both numbers is", n3)
print ("")
print ("")
n4=(x/y)
n4= int (n4)
print ("The division between both numbers is", n4)
print("")
print("")
n5=(x%y)
n5=int(n5)
print ("The modulus operator between both numbers is", n5)
print("")


#TC1014
#WSQ01

No hay comentarios:

Publicar un comentario