Quiz Week 3

Here we are going to create a program with two functions:

  • def square_root(x):  // returns the square root of x (float)
  • def cube_root(x): // returns the cube root of x (float)

We should ask the user for a number and then, print the square and cube roots out.

So, here is the code:

captura-de-pantalla-2017-01-30-a-las-20-49-29

First of all, we need to import math that is like call the library to come here (we need it, because it contents the functions). After that we write at the beginnig the functions we want. In this case I didn’t know how to code cube root, but remember that if we raise a number to one third it’s the same, so that’s what I did. We write def and the function. Then, in the next line what do you want it returns.

To continue, we start with our main code. We save what the user will introduce like an int and we are going to call it “x“.What about if the user enters a negative number? Well, I decided to send him a message, but if not, I continue with my programm (that is what we are evaluating in the if-else).

Now, to print the roots, first we have to make the operations, that is what we are doing in the z and y declaration. Finally, we just write a message to announce what we are doing plus the result.

And it’s ready 🙂 It runs like this:

captura-de-pantalla-2017-01-30-a-las-20-51-31

Mastery topics: Use of comments/Validated user input (ensure correct/expected data entry)

Leave a comment