#WSQ09

 

For this assignment we needed to make a factorial calculator. The factorial of a number is the multiplication of all numbers that are lesser than that number until 0. For example the factorial of 4 is equal 4x3x2x1=24

This program required loops as we couldnt use the default python function from the math module.

 

-So first I used while loop so the user can use the program as many times he like. 

res=”x”
while (str (res) != “n”):
print (“This program is a factorial calculator”)
n= int (input (“Please give me a positive integer number: “))

 

-Then I implemented a for loop in order to calculate the factorial of any given number. 

for n in range (1,n+1):
num= num*n
print(“The factorial of the given number is: “,num)
print (“”)
print (“y= yes, n= no”)
res=str(input(“You wish to calculate another factorial of a number? “))

 

-And finally I used an If so my loop could work as I wanted.

if (res==”n”):
print(“”)
print(“Have a nice day!”)
else:
print(“”)

 

The final code:

fac

 

The program:

fac2

 

 

 

Btw, I´m not sure if I completely understand how indentation works on python and on my Text editor.

 

 

 

 

 

 

 

Quiz 3

Program 1:
x1= int (input (“Give me the value of x1: “))
y1= int (input (“Give me the value of y1: “))
x2= int (input (“Give me the value of x2: “))
y2= int (input (“Give me the value of y2: “))

def distance (x1,x2,y1,y2):

d= (((x2-x1)**2)+((y2-y1)**2))**0.5
print (“The distance is: “)
return d

print (distance(x1,x2,y1,y2))

 

Program 2:

n=int (input(“Give me a number so I can give you the equivalent in the fibonacci series: “)
def fibonacci (n):
if (n==0):
return 0
elif (n==1):
return 1
else:
return fibonacci(n-1)+fibonacci(n-2)

print(fibonacci(n))

 

 

Quiz 2

Program 1

a= int (input(“Give me the number that will be powered: “))
b= int (input(“The power: “))
def superpower (a,b):
c=a**b
return c

print(“The result is”, superpower(a,b))

 

Program 2

a= int(input(“Numbers of stars you want: “))
def stars (a):
print(“*”*a)
return “”
print(stars(a))

 

quiz2

 

quiz22

 

#WSQ08

The functions are an important part of programming, apparently. Ken says we have to get used to using them and I probably will, some day, I hope. The bright side is that is really easy to define and call functions.

 

For this program we had to go back to the fun with number assignment, but now we are tasked to do the same operations with functions.

 

 

-Creating a function requires to have some established variables, then you have to select the parameters, after that you need to write the statement of the function, and finally you have to write the return statement or the result.

def res (a,b):
n = a – b
print(“The difference of those numbers is: “)
return n

def product (a,b):    
n= a*b                
print(“The product of those numbers is: “)
return n

 

-After you have defined the function you can call the function by writing the name of the function next to the parameters inside a parenthesis.

print (res (a,b))
print (product (a,b))

 

n21

n2.png

 

The program is this:

functions

 

 

 

#WSQ07

This time we had to create a program that shows the result of a inclusive sum of numbers in a random range that the user has to pick.

 

-First we have to make sure that the user types the lower and higher number the range correctly, that means tha the lower number has to be lesser than the last number of the range.

x=0
y=0
print (“This program will calculate the sum of integers in the range you provide”)
while (int (x) >= int (y)):
x=int( input(“Please give me the lower bound of the range:”))
y= int (input (“Please give me the upper bound of the range:”))

if (x>=y):
print (“The lower bound has to be smaller than the upper bound”)
print(“”)

 

-Then we have to make our code sum all of the number between that range, in order to do so we have to use a for loop. 

num = 0
for n in range (x, y +1):
num= num + n

 

 

 

So the final code looks like this: 

sumoftl

 

And this is the program running. 

sum

#WSQ06

 

 

 

 

 

 

This code required to import a module, but what is module?  you might ask. The answer of that question is inside this page: https://docs.python.org/2/tutorial/modules.html

Basically a module is script or python file that cointains several functions and statements. You can import modules to the main module of python and you can also create your own. Most of the functions and statements we need by now, already exists within the python modules. 

 

But let´s get back to the assignment; for this program we need to ask the user for a random number until he guesses the exact same number that the our code has chosen randomly.

 

 

-First we need to import the random module, with this module we can use a function that selects a random number of a certain range.

import random

num=(random.randint(1,100))

 

-After asking the user to try to guess the number, we need to impliment a while loop so the user can  have many tries until he guesses the number. 

print (“I have a number chosen between 1 and 100”)
print (“Try to guess that number”)
countg=”guesses to get the right number”
guess=0
count=0
while int (guess) != int (num):
guess=input()
count= int(count)+1

-This code means that when the user inputs the right number the loop will end and the program will be over.

 

-But that is not it, we also need to tell the user if his guess is lower or higher than the actual number. To do this I used If and else conditions. 

if int(guess) > int(num):
print ( guess,”is too high, try again”)
if int(guess) < int(num):
print(guess,”is too low, try again”)

print(“You guessed the number!”)
print(“It took you”,count,countg)     

-I also used a counter

random
guess

Continue reading “#WSQ06”

#WSQ05

 

With this program I learned the basics of the conditionals in python.

The if, elif and else are functions that evaluate certain conditions and if such condition is true, the program executes a certain code, that code would be the statement of the condition. There can be multiple if, elif and else in your code. 

This page is helpful for understandig this concept- https://docs.python.org/3.5/tutorial/controlflow.html

 

For this program we have to ask the user for a temperature in Farenheit. Then the program has to convert that value to a temperature in Celsius. Also we have to tell the user if water boils or doesn´t at that temperature

The formula for converting Celsius to Farenheit is:

Cº= 5*(Fº-32)/9

Doing that is the first part of the code, and probably the easiest part. 

print (“What is the temperature in Farenheit?”)
temp=input()
deg=”degrees”
cel= 5*(int(temp)-32)//9
print (“The temperature in Celsius is:”, cel, deg)

The next part requires the use of the if and else functions.

if cel < 100 and cel > 0:
print (“Water does not boil at this temperature (under typical conditions).”)
if cel > 100:
print (“Water boils at this temperature (under typical conditions).”)
if cel<0:
print (“Water freezes at this temperature”)

The complete code would look like this:

 

temp

And the program running, like this:

 

temp2

 

 

#WSQ03

This assisgment was a little bit trickier than the previous one, but still is fairly simple. All I needed to do is to ask the user to type 2 numbers and then make 4 different operations with such numbers.

For knowing how to write this code I again read some parts of the “Think Python” book.

http://www.greenteapress.com/thinkpython/thinkpython.pdf

I had a doubt about how to do the integer division so I searched online for the answer and this web page gave me the solution.

http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division

 

Here is my final code:

print (“This program will do some operations with 2 numbers you choose”)
print (“Type a number”)
num1=input ()
print (“Now type another number”)
num2=input ()
sub=int(num1) – int (num2)
multi= int(num1) * int (num2)
div= int(num1) // int (num2)
rem= int(num1) % int (num2)
print(“The difference of those numbers is”, sub)
print(“The product of those numbers is”, multi)
print(“The division of those numbers is”, div)
print(“The remainder of the division of those numbers is”, rem)

 

And this is the program:

numbers

numbers2

http://www.greenteapress.com/thinkpython/thinkpython.pdf

#WSQ02

 

This was the first time I made a Python program ever. 

In order to know the basics of coding with python i read the textbook that is on the TC101 web page of this course. Specifically the first and second chapter.

http://www.greenteapress.com/thinkpython/thinkpython.pdf

 

think python

 

My main problem with this assignment wasn’t writting the code but instead it was running the program in the terminal. With the help of Mr. Ken I finally learned how to make the cygwin terminal actually open any python program. 

 

hello

world

 

I just needed to tell cygwin the directory of where i saved my code and that was it.  

directory

http://www.greenteapress.com/thinkpython/thinkpython.pdf

http://www.greenteapress.com/thinkpython/thinkpython.pdf

WSQ01: Get Coding

 

The first text editor  I installed was atom, mainly because it looked cool and good for begginers.

atom

 

but after doing some research i came to the conclusion that the best text editor for me would be Sublime Text 2 because it has everything I’ll ever need. 

 

sublime

 

After that I installed Python 3 using cygwin.

cygwin

 

Therefore I’ll be using the cygwin terminal to run my Python 3 programs.

 

terminal

 

And that is pretty much my whole coding enviroment (for now at least).

 

my-code-doesnt-work-i-have-no-idea-why