Tag Archives: #awesome

Mastery12 – Creating python functions

Creating a function in python is not hard, give it a name and give it a job.

to create it first we need to define it, giving it a name and a number of variables it needs.

def  adding_function(a,b):

and inside the function the operation it will perform, this function will add a + b and return the result.

def  adding_function(a,b)

       return a+b

now call the function to add two numbers.

reference:

http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6/Defining_Functions

Mastery11

In python to call a function write the name of the function and the variables it needs, for example:

function_name (x,y)

and you can call a function to give value to a variable.

z = function_name(x,y)

Mastery10 – Basic output: print in python

Telling a program to print is to display information on screen. to use it in python write print() in the parentheses write the data you want to see on the screen.

example:

print (“hello world”)

the program should display “hello world” on the screen when you run the program, also it can print the value of a variable:

x = “hello world”

print (x)

and “hello world” will apear on the screen, not “x”.

Mastery09 – Basic types and their use in python

the basic types of data that python works with are:

1.- Integer: these are whole numbers from negative to positive, they can be used in the program simply writing the number in the program like 45 and to turn an input into an integer write int before the input.

2.- Float: floats are numbers with decimals like 2.5, to turn an integer into float just write float before it.

3.- strings: a set of letters, number or other characters, these dont hold any value appart from the character itself, a  number written as a string wont be counted as a numeric value. strings are between ” ” to differentiate them.

4.- Tuples: a list with a fixed number of elements, they are use ( ).

5.- List: a list without a fixed number of elements, they use [ ].

6.- Dictionaries: a list of multiple elements that can be adressed by text, they use { }.

reference:

http://en.wikiversity.org/wiki/Python/Basic_data_types

WSQ11 – Yo soy 196

this program recives two number values, a low number and a higher number, and check the numbers from the lower to the highest number reporting: the range of numbers analyzed, number of natural palindromes, number of non-lychrel numbers, the amount of lychrel numbers found and print the lychrel numbers found.

to start i created a function to invert the integers so i could compare them.

def reverse(a):

        a = str(a)

        a = a[::-1]

        a = int(a)

        return a

this turns 123 into “123” into “321” into 321.

a function to determine if a number is a natural palindrome.

def palin(a):

      a1 = a

      a2 = reverse(a)

      if a1 == a2:

          return 1

      else:

          return 0

and a function to add the numbers to their inverse until they become a palindrome or reach a set amount of tries.

def nopalin(a)

    c = 0

    while c < 30:

       a1 = a

       a2 = reverse(a1)

       a3 = a1+a2

       a4 = reverse(a3)

       if a3 == a4:

          return 1

          break

       if a3 != a4:

          a = a3

          c += 1

       if c >= 30:

           return 0

that will take a number, invert it and add them until it becomes a palindrome for 30 tries, if it does not becomes a palindrome before then it will count as a lychrel number. after those 3 functions all you need is arrange them and add counters an a print order for the lychrel number found.

 

reference for the reverse function:

http://stackoverflow.com/questions/931092/reverse-a-string-in-python

my code in github:

https://github.com/nazare52/progra/blob/master/wsq11.py

WSQ08 – on to functions

remember WSQ03? we had to create a program that asked for two integers and printed the sum, difference, product an stuff. now we will do it again but with functios to calculate those values.

to create funcitons in python we have to define it giving it a name and variables and what we want the function to return:

def suma(a,b):

      return (a+b)

ask the user to input two integers and to call the function do this:

a = int(input())

b = int(input())

print  suma(a,b)

make a function for each operation and tell the program to print it.

my code:

https://github.com/nazare52/progra/blob/master/wsq08.py

WSQ07 – sum of numbers

the program has to sum all the integers in range between two numbers given by the user. at first I did it with the range function but the teacher asked for a loop to make the operation so I did it like this.

ask for two integers, one low and one high but if the user enters the a lower number in the high number place the program wont work properly so I added a condition to correct the numbers:

x = low

y = high

if x > y:

    x,y = y,x

that will exchange the values between the x and y and so continue the program correctly. now to do the sum i added a variable with a value of 0 and used a while loop:

z = 0

while x != y:

   z = z+x

   x += 1

that will add x to z and rise x one number to keep the loop until x=y but will stop before the last number, if x=1 and y=5 then the operation would be 1+2+3+4 excluding the high number, to fix this I added a last operation at the end to print the sum the resulting z with the high number y:

print (z+y)

and it’s done.

question – how do I tell the program to ask again for an integer if the user enters a string?

my code in github:

https://github.com/nazare52/progra/blob/master/wsq07.py

 

WSQ06 – pick a number

this time the program has to pick a number at random from 1 to 100 and ask the user to gues the number, after the user makes a guess the program will give hints if the number the user gave is too high or too low in relation with the number picked by the program.

for the program to choose a number at random you can use the random function of python:

import random

and give a variable for the value, if we want a number from 1 to 100 set the range (1,101) because the range goes from the first number to the number before the last.

import random

x = random.randrange(1,101)

with that you should get a random number between 1 and 100. now ask the user for an integer to continue with the program and open a “while” loop so the program doesn’t stop until the number from the user is the same as the random:

y = int(input(“number”))

while y != x:     ——- (“!=” means not equal to)

and to give the hints to help the user use if statements and ask for anothe number:

if y > x:

   print (“too high”)

   y = int(input(“again”))

make another for a number below the random. when the user hits the number tell the program to print it:

print (“correct it is “,y)

if you want to add a counter to know how many times you tried make a variable with 0 and do this inside the while loop:

z = 0

while y != x:

    z += 1

and tell the program to print the result at the end:

print (z)

my code is in github:

https://github.com/nazare52/progra/commit/2515ed92aae442c058c6c1c325ed6bbe2a24cb0d

this video helped me to understand loops in python:

https://www.youtube.com/watch?v=9AJ0uoxtdCQ

 question- how do I tell the program to run again after completing the condition of the while loop?