WSQ03 – Pick a number

--Originally published at Stuff about Python

Hi there, today in response to WSQ03, I wrote a small program that defines a random number between 0 and 100, and lets the user try to guess it while giving him a hint whenever he is wrong. Here it is :

wsq03

I imported the randint() function from the random module, that lets you define a random integer in a range chosen.

After defining that principal number, n, I let the user try to guess it using the input() function, and check with conditional statements if the number entered is the same than the random number.

If it is, the user wins. Otherwise, the number of guesses is kept track of, and a hint is given : ‘too high’ or ‘too low’.

 


Quiz W4 -Minimum and squares

--Originally published at Stuff about Python

And second quiz today, for week 4 this time. Same idea, using functions to compute the minimum and the sum of squares of three numbers.

Here were the instructions:

For this quiz I want you to (in class) create a program with two functions:

  • def minimum_three(x, y, z):  # returns the value that is smallest of x, y and z
  • def sum_squares(x, y, z): # returns the value of the sum of squares of x, y, z

This the solution I’ve come up with :

quiz4

It uses the built-in function min() that returns the smallest number from a bunch of numbers, and uses the same idea than on quiz w3 to check for the user input to make sure it can be converted to an integer.


Quiz W3 – Functions

--Originally published at Stuff about Python

Earlier this week (woops, it’s been a while), we got a quiz in class, here are the instructions:

For this quiz, 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)

 

The whole point of this quiz was being able to create and then call functions, so I created the functions needed to perform the actions required, as you can see in the solution below.

quiz3

The square_root function uses the math module, which I imported previously, to get the square root of a number more easily.

The cubic root is then computed in another function.

The run() function is my way of making sure the user enters an input that can be converted to a number, and if not, it will raise an error and try again.


WSQ01 – First resolution ever

--Originally published at Stuff about Python

Hi there,

So we were given the following instructions for this first little problem :

Ask the user for two integer values, then use those two values to calculate and show the following:

  • The sum of the two numbers.
  • The difference of the two numbers.
  • The product of the two numbers.
  • The integer based division of the two numbers (so no decimal point). First divided by second.
  • The remainder of integer division of the two numbers

Which I answered with the following (quite long) bit of code, which is a bit more than necessary I assume. wsq01

So the initial idea behind the assignment was to cover basic user input (asking for 2 numbers) along with basic output (printing the results of the operations).

The user input is dealt with with the built-in function in Python 3, input(), which will wait for the user to give a value to continue. You can actually add some text to it to let the user know what you’re expecting from him. Here I asked on line 2 and 3 to enter a “First value” and a “Second value”.

I performed operations on those values, such as adding, substracting, multiplying and dividing them, as required in the instructions, and used the print() function to display them to the screen.

To perform all of this, I created a get_results() function, which allows me to use all this code anytime I want later on, without having to re-type the whole thing.

The thing that bothered me though, was that if you entered something else than an integer when asked for a value, you would just crash the program because it wouldn’t be able to perform all the operations. Yeah, you can’t really substract a “cat” and a “zebra”. So what I did about it is I created another function

Continue reading "WSQ01 – First resolution ever"

Hi there.

--Originally published at Stuff about Python

22857675

Hi there, let me introduce myself,

I’m Victor, a student from Iéseg, school of management in France. I’m currently in exchange in Guadalajara, and I went for this intro to programming class, even though it’s got nothing to do with my studies.

I’ll be posting here the solutions I find to the problems given to us in class, feel free to let me know what you think !

Victor

https://memegenerator.net/instance/22857675