--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.
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" →