In: Computer Science
Respond to the following in a minimum of 175 words:
One of the most important concepts of programming is handling input and output. The following activity will allow you to get familiar with this concept specifically when using Python.
Write a function to add two values and display the results.
Discuss the steps in your thought process as you created the code, any issues you encountered, and how you solved those issues.
PLEASE FIND THE ANSWER(S) and EXPLANATION BELOW.
======
When we don't check for the strings...
CODE:
# function that adds two values and display the results. def add(a, b): # calculate the sum total = a + b # print/ display the result. print('The sum is:', total) # Test the function here # Execution starts from here # Read numbers number1 = input('Enter a number: ') number2 = input('Enter another number: ') # call the add function add(number1, number2)
===========
When we try to add one and two, we get onetwo not three as output..!!
So, we have to give the numbers, not the strings.
Below is the correct way of doing this:
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. # function that adds two values and display the results. def add(a, b): # calculate the sum total = a + b # print/ display the result. print('The sum is:', total) # Test the function here # Execution starts from here # Read numbers number1 = input('Enter a number: ') number2 = input('Enter another number: ') # when we read input, it is in the form of strings, # So, convert them to strings as follows. number1 = int(number1) number2 = int(number2) # call the add function add(number1, number2)
=================