jueves, 26 de febrero de 2015

#WSQ09

click here for code

in this program we are doing the factorial multiplication of a number:

I crated a fuction to do so, using a counter and an accumulator within a loop. the program also uses many while and if statements, so the program ca repeat itself until the user wants to stop.


jueves, 19 de febrero de 2015

#Mastery11 how to call a fuction.

Calling a fuction its exactly what it sounds like. You are telling the fuction to act on the values you have stablished.  to call a fuction  first you must have a variable that will get the return value of your fuction.

example:

lets say you crated a fuction called: make_sum.

the fuction would look like this:

def make_sum(x,y):
ans=x+y
   return ans

so in your code you would create a varible, lets say:

the_sum =

then you write the fuction, adding the values that you want it to take.

the_sum = make_sum(2,5)

so: 
make_sum(x,y):
ans=x+y
   return ans
will become:
make_sum(2,5):
ans=2+5
   return ans

and the variable the_sum will receve the return value of:


make_sum(2,5):
ans=2+5
   return ans
wich in this case is 7.

The Fun with numbers code in here shows an example of a program that calls differnt fuctions.

note: is very usefull to create your fuctions in a separate place from the rest of the code.

miércoles, 18 de febrero de 2015

#mastery12 fuctions in python


To define a fuction you write the word def followed by the name you gave your fuction, in parethes you grite the values it will take, a colon ":" and in the folowing lines you write the steps the fuction will follow. at the end you write return and the value that it will end up giving. 

example:
def make_sum(x,y):
ans=x+y
   return ans



this program shown here uses personally made fuctions:

you can copy and paste the code from this link to try it on your own.  


Fun with number, now functions in Python.

#wsq08

this is a remake of the fun with numbers program, but this time using fuctions:

click here to see the code

check out mastery 12 to see how to make your own fuctions.





martes, 10 de febrero de 2015

#Mastery20 'for' loops

"Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence."(Original text.)

In this case the loop of for prints over the sequence of numbers given by the user. Due to the way that python sets up sequences it was real E-Z to make the user determine the way that the numbers will go up. The range of numbers is exclusive, that means that it wont include the very last númber of the sequence, I represented that in the program with a '>'. 

example: 
for i in range(0,11,5):
     print (i)
In this case the program will loop until every single value of i is printed. i is in range from 0 to 10, and it will go up by 5. Which means that it will appear: 0,5,10. 

Use of "elif"

#mastery17.
The elif statement is a conditional statement that is short for 'else if'. It goes right after an if statement and is good so you can prevent exesive identation.
This qwick program uses both the if and elif statements, as well as an final else stament.

I hope this was usefull in your if statesment programing.  

To learn more about if staments Click Here.

lunes, 9 de febrero de 2015

#WSQ07 Sum of numbers

This was quite a fun program to make. I used a While loop, you can see the code and the terminal runing in the picture, as well as a online calculator prooving that that the answer is right.

Using a While loop

A while loop is used in a similar fashin that the conditional loop, exept this will run every single time that the condition in it is found to be truth.

So you have your basic stuff. You grite your conditional inside parenthesis followed by a colon, and then you write all the code you want the program to repeat till the condition is truth in the following line with an identation.

example:

x = int(input("give me a number"))
while(x!=0):
           if(x>0):
                  print(x," is a positive number")
           if(x<0):
                  print(x," is a negative number")
           x=int(input("Change the number: ")
if(x==0):
          print(x," is cero")

In this case the program wont stop (it wont get out of the loop) untill you tipe the number "0". 

Again the Random númber program is a great exaple using the while loop.


Use of the conditional IF

The random numbers program in here shows a great example of the If conditional being used. You basically set your conditinial, write a colon, and in the next lines you write the code with an identation of all the steps that will follow beeing the conditional truth. 
example: 
x = int(input("give me a number"))
if(x>0):
           print(x," is a positive number")
if(x<0):
          print(x," is a negative number")
if(x==0):
          print(x," is cero")


Coments in Python

It's real simple. You write whatever you want to coment right beside your code, sandwitched in between two '#' simbols.