miércoles, 22 de febrero de 2017

Sum of numbers

Sum of numbers
For this task we needed a code that asks for a range of integers and then prints the sum of the numbers in that range (inclusive). You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

1.- First write the upper and lower bound:

print ("Sum of Nubers")

x=int(input(("Give me the lower bound")))
y=int(input(("Give me the upper bound")))
sum = 0

2.- State the range of the numbers using the "for a in ()" function:
for a in range(x,y+1): this states the number of times you want the sum of numbers to happen
    sum+=a
print ("The sum between the numbers is",sum)

Check out http://www.python-course.eu/python3_for_loop.php for more info and to find some help


#WSQ04

Pick a number

Pick a number
For our #WSQ03 task we were asked for a program that picks a random integer between 1 and 100, the it prompts the user for a guess of the value, and it will hint "too low" or "too high". The program will continue to run until the user guesses the integer. As an extra credit we were asked to show the user how many tries it took him to guess the number.

1.- For this exercise we need to import the "random library" by typing "import random", with these we can use the random function just like this:

import random
n=random.randint(1,100)   notice how the function has to be written. 
print("Shhhhh. the number is",n)  this is for you to know what the number is, so it will make it easier test your code.

2.- Ask the user for an integer:
print ("I have choosen a random number between 1 and 100, can you guess it?")
guess=input("Please write a number")
guess=int(guess)
count=1  since we want to show the user how many guesses it took him to guess the answer , we need to declare a variable and give it the value of one.

3.- Now we have to make a loop function to make it go over the process over and over again until the user guesses the number:
while n!=guess: this tells the program to keep making a cycle while n is different from the guess
    count=count+1this is the counter
    if guess>n:
        print ("too big")
    else:
        print ("too small")
    print("Keep trying")
    guess=int(input("Try another number"),)

print("it took you",count, "guesses")

lunes, 20 de febrero de 2017

TEMPERATURE

TEMPERATURE

For this task we were asked to create a program in which the user will prompt a temperature in Fahrenheit and convert it into Celsius The program also had to sate if water would boil at the temperature given by the user.

In this exercise we´ll be using function to strength the knowledge from the past post.

1.- First define your function and make it return a value:
                 def fahrenheit (x):
                      return (5*(x-32)/9)
2.- Declare your variable, remember to make an input on it so the user can type a value:
x= float(input(("Temperature you would like to convert")))
c=fahrenheit (x)

3.- Print the answer:
print ("That´s", c, "celsisus degrees")

4.- Use an "If" function to get the program to tell you if water will boil or not:
if x<212:
    print ("Water does not Boil at this temperature")
else:
    print ("Water will Boil")




Complete code: 

print ("Converting Fahrenheit to Celsius")
def fahrenheit (x):
    return (5*(x-32)/9)

x= float(input(("Temperature you would like to convert")))
c=fahrenheit (x)

print ("That´s", c, "celsisus degrees")
if x<212:
    print ("Water does not Boil at this temperature")
else:
    print ("Water will Boil")



#WSQ02










miércoles, 8 de febrero de 2017

Functions (Quiz3)

Functions 

For today´s class we had Quiz 3, which consisted on creating two functions to get the square and cubic roots of any number. This was pretty simple:

First as always, you have to open your editor and save your work as "Quiz3.py".

For this exercise we need to use the math library by using the "import math" function before starting our code.

Now, you have to generate your function, this is pretty simple and I found some help on how to do it in the How to think like a computer scientist interactive book (I´ll leave a link at the end of this post).

  • First you must define the function just like this: def square_root (x): 
  • Then you have to ask your computer to return the value or result of that function:return math.sqrt(x) (FOR THE CUBIC ROOT FUNCTION YOU HAVE TO USE THE "MATH.POW")
After defining your functions, you need to make an input into your code so you can type the number you want to use. x= float(input("Type a number:"))

For this final step you just have to give a variable to the functions r= square_root (x)  so you can ask the code to print the results. print ("The square root of",x, "is", r).

Here´s the complete code:
import math

def square_root (x):
    return math.sqrt(x)
def cubic_root (x):
    return math.pow(x,(1/3))


x= float(input("Type a number:"))
r= square_root (x)
c= cubic_root (x)

print ("The square root of",x, "is", r)
print ("The cubic root of",x, "is", c )

The link to the book: http://interactivepython.org/runestone/static/thinkcspy/GeneralIntro/toctree.html

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

domingo, 15 de enero de 2017

FIRST WORDS IN PYTHON 3

This post is about how to make your first "script" min python3 and to print it in your computer's terminal. (Only for mac users)


We will start with something easy and very common, the famous "hello world" line, but first you need to prepare your computer for this, which means you have to install the right softwares to make this run.

1.- You need to go to home brew (http://brew.sh) and copy the script line that appears on the main page.

2.- Open your Mac´s terminal and paste the script line which should look like this:


(It will start installing some packages and other stuff in your computer, which will be all necessary)

3.- After it finishes installing everything you should be able to type, so you´ll write "brew install python3".


4.- After it has finished, you'll need to go to your browser and look for atom, which is a text editor that will help you make your code. (https://atom.io)

5.- Now that you have atom installed, open it, go to "File" on the left top corner and choose "new file"

6.- Click on save as and name your file "hello.py", save it in your desktop. (we use ".py" to tell the computer that we´ll be writing in python)


7.- Type print ("Hello world") and save the file.

8.- Go to your terminal and type "cd Desktop", this because you were supposed to save the file in your desktop, and with this command the terminal will go to your desktop and look for any files that you have saved there.

9.- Now that you are in your desktop, you have to type "python3 hello.py" and the terminal will print the hello world phrase.