Quiz # 5

--Originally published at Python learning

Quiz number five consisted of the first five exercises of the following list of Python problems:

http://www.ling.gu.se/~lager/python_exercises.html 

The first two problems were about returning the largest number introduced by the user, something I had already done in other practices and I have posts about. Here they are:

First problem:

quiz5-1quiz5-1run

Second problem:

quiz5-2quiz5-2run

Problem three was about giving the length of a string, like this:

quiz5-3

quiz5-3run

Problem four consisted of outputting True if a character introduced by the user was a vowel, and False if it was otherwise:

quiz5-4

quiz5-4run

Problem five was a little more complicated. I had to, as said textually in the page, “double every consonant and place an occurrence of "o" in between.” It took me a little more time to understand how it was done, but at the end it looked like this:

quiz5-5

quiz5-5run

And that’s it for this quiz!


Pick a number!

--Originally published at Python learning

This time I’m going to show a program that chooses a random number from 0 to 100 and then asks the user to guess this number. To make it easier to guess, it gives clues by stating if the number entered is higher or lower than the one chosen. Also, when finally guessed, it says how many tries it took the user to do so.

In order for me to learn how to do this, I checked the following page: http://www.pythonforbeginners.com/random/how-to-use-the-random-module-in-python

At first I had a small problem because I wanted to use ‘try’ as a variable name, but I forgot in python that one is a keyword, which are specific words already used in order to tell the program what to do. So naturally, when I tried to run it my program marked SyntaxError: invalid syntax.

Once I realized the mistake I had made, I changed the variable to ‘number_guess’ and everything was fine! Here is where I got more information about keywords so I wouldn’t make that mistake again: http://www.pythonforbeginners.com/basics/keywords-in-python

After that, this is how I wrote my code:random

And it runs like this:

random-run

 


Quiz # 4

--Originally published at Python learning

For this quiz I had to make a program that would print the smallest value and the sum of the square of three numbers given by the user (x,y, and z).

I decided to use conditionals to get the smallest value, like this:

if      x <= y <= z     or     x <= z <=y:
return x
elif   y <= x <=z    or    y <=z <=x:
return y
else:
return z

At first I didn’t put the equals in the equation, so there was trouble when two inputs were the same. However, I then added them to my code and everything was fine! When I was done this is how it looked:

quiz4

And this is how it runned:

quiz4-run


Quiz # 3

--Originally published at Python learning

For this quiz I had to make a program that would return the square and cube root of a given number. I used two mathematical functions: math.sqrt() for the square root and math.pow(); which raises a value, in this case to 1/3 to get the cube root. I also had to first import math module.

This is how my code looked at the end:quiz3

And how it runned:

quiz3-run

 


Sum of numbers: loops and conditionals

--Originally published at Python learning

Here I am going to show you a program that sums a range of numbers provided by the user. For this problem I used a loop, and I learned to do so in python from here: http://www.python-course.eu/python3_loops.php 

I also had to use conditionals to make sure the first number the user entered was lower than the second, in order for the program to work correctly. Here is a flowchart explaining how conditionals work:

flowchart_if_else

Which I got from another page that helped me understand both conditionals and loops:

http://www.openbookproject.net/books/bpp4awd/ch04.html

At the end, my program looked like this:

sum

And runned like this:

sum-run

 

 


Temperature!

--Originally published at Python learning

In this post I’m going to show a program that will convert a given temperature from Fahreinheit to Celsius, and then state whether or not the water would boil at this temperature.

To do this I decided to use a function. Even though it is not that necessary to do it like this, I thought it was a good way to start practicing using functions.

Defining a function in python looks like this:

function

You can read more about that from the page I got it from: http://hcc-cs.weebly.com/functions.html 

So, to solve this problem I defined the function like this:

def Celsius(x):
return 5*(x-32)/9

And this is how my whole problem ended up looking:

temperature

Which runs like this:

temperature_run


Fun with numbers!

--Originally published at Python learning

Here I am going to show a program that asks the user for two numbers (integers) and then displays the sum, difference, multiplication, division (with no decimals) and remainder of these numbers. In this program you will be able to identify the inputs and outputs, which I already talked about and explained in a previous blog.

The code for this is as follows:numbers

And it runs like this:

numbers-running

So here, the inputs are 12 and 10, and the outputs are 22, 2, 120, 1 and 2.


Input and output

--Originally published at Python learning

In a computer program, an input is what the user enters when it is required. For example, you can ask for a name in order to display the phrase Hello, and the name provided by the person. In this case, the name would be the input. On the other hand, the output is the result the program gets after the input is given. For example, if the name was Gina, the output would be Hello, Gina. 

In python, this is how we would do the example:

name=input(“What is your name?”)

print(“Hello, “,name)

We use the symbol = to assign a value to a variable. You can learn more about that here: https://www.tutorialspoint.com/python/python_variable_types.htm 

input_output

From: https://www.tes.com/lessons/ZnhIVryvqXaUVg/inputs-and-outputs


Starting with Python!

--Originally published at Python learning

Python is a high-level programming language. This means it uses natural language elements to program instead of ones and zeros, making it easier and more understandable.

It’s also an interpreted language: it uses an interpreter to execute the program directly instead of using a compiler to translate it into machine code. It was created by Guido van Rossum and released in 1991.

To program using Python on Windows, I installed Cygwin (a tool that provides a Linux-like environment) and Atom (a text editor) but it can be any other like Notepad++ or Sublime Text. Here is a know-how on installing these software for Windows users: https://www.davidbaumgold.com/tutorials/set-up-python-windows/

The first program one usually does when learning a new language is the output “Hello, World!”. In Python, this simple command line looks like this:

hello

And runs like this:

hello-world