WSQ01 – First resolution ever

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


def get_results():
value1 = int(input("First value : "))
value2 = int(input("Second value : "))
summed = str(value1 + value2)
difference = str(value1 – value2)
product = str(value1*value2)
division = str(value1 // value2)
remainder = str(value1 % value2)
print()
print("The sum of the two values is",summed +".")
print("The difference is",difference +".")
print("The product is",product +".")
print(value1,"divided by",value2,"is",division +".")
print("The remainder of",value1,"divided by",value2,"is",remainder +".")
input()
def running():
try:
get_results()
except ValueError:
print("Wrong type of value, try again.")
running()
print("Hi there, hit me up with 2 integer values.")
print()
running()

view raw

WSQ01.py

hosted with ❤ by GitHub

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 running(), that will call the first function and make sure it doesn’t crash. In case something else than integers is entered, the get_results() function will not be able to run, so the running() function will just print “Wrong type of value, try again.” and start over, until the 2 values entered are integers and the function get_results() is carried out.

This is definitely not the perfect way to do it, so let me know how I can improve it!

Thanks for reading,

Victor

Hi there.

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