Question

In: Computer Science

Respond to the following in a minimum of 175 words: One of the most important concepts...

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.   

Solutions

Expert Solution

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)

=================


Related Solutions

Respond to the following in a minimum of 175 words: One of the most important concepts...
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. (Please show me where indentation goes as well, I'm not only seeking guidance on answers I'm trying to understand this so I can learn). Discuss the steps in your...
Respond to the following in a minimum of 175 words: It is important to program your...
Respond to the following in a minimum of 175 words: It is important to program your code efficiently. Efficient code manages errors and exceptions and cleans up memory after it ends. The try-except statements are helpful in handling errors that are detected during execution. What are the two categories of errors when debugging code? How can the try-except statements handle errors in Python? Provide a code example that supports your comments.
Respond to the following in a minimum of 175 words: Why is it important to keep...
Respond to the following in a minimum of 175 words: Why is it important to keep your product in the growth stage for as long as possible?
Respond to the following in a minimum of 175 words: The most frequently used measures of...
Respond to the following in a minimum of 175 words: The most frequently used measures of central tendency for quantitative data are the mean and the median. The following table shows civil service examination scores from 24 applicants to law enforcement jobs: 83 74 85 79 82 67 78 70 18 93 64 27 93 98 82 78 68 82 83 99 96 62 93 58 Using Excel, find the mean, standard deviation, and 5-number summary of this sample. Construct...
Respond to the following in a minimum of 175 words: Explain why responsive design is important...
Respond to the following in a minimum of 175 words: Explain why responsive design is important in today's landscape. What do websites risk if they decide not to address responsive design?
Respond to the following in a minimum of 175 words, please type response: An important part...
Respond to the following in a minimum of 175 words, please type response: An important part of using statistics is being able to explain your results to decision makers. Imagine that you have conducted a two-sample test and determined that the difference was not statistically significant.   While one mean was 4.3 and the other was 3.9, the p level for the t test was p=.07. Your management team says, “Well, the difference may not be statistically significant, but the difference is...
Respond to the following in a minimum of 175 words: Consider your current or most recent...
Respond to the following in a minimum of 175 words: Consider your current or most recent position. What are some ways, if any, in which your organization encourages diversity? In your opinion, what are some areas of opportunity with regard to addressing diversity within your organization?
Respond to the following in a minimum of 175 words: Most manufacturing organizations have a service...
Respond to the following in a minimum of 175 words: Most manufacturing organizations have a service component which, if not incorporated into the business, will jeopardize the survival of the business. Discuss the difference between the service industry and the manufacturing industry. How can you determine which organizations fall into which industry?
Respond to one of the following in a minimum of 175 words: 1) Discuss: What are...
Respond to one of the following in a minimum of 175 words: 1) Discuss: What are the limitations of GDP as a measurement tool?
Please respond to the following in a minimum of 175 words: Winning one of the big...
Please respond to the following in a minimum of 175 words: Winning one of the big national lotteries provides a choice of how to receive the money. You can receive a much-reduced lump sum (like half or so) today, or the full amount in twenty annual payments starting today. Discuss how you can determine which financial deal is better. Also, what are the non-financial aspects of winning the lottery and how do they influence which option to take?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT