Question

In: Computer Science

Task 2: Debugging and writing functions In IDLE, use the File -> New Window menu option...

Task 2: Debugging and writing functions

  1. In IDLE, use the File -> New Window menu option to open a new editor window for your program, and save it using the name hw4Task2.py. Make sure to specify the .py extension.
  2. Copy the following (buggy) code into that file:

def mysum(x, y)

""" takes two numbers and returns their sum """

    total = x + y

    print(total)

  1. Save the file using Ctrl-S. Then try to run it using F5. What happens?
  2. This function includes two syntax errors – errors that violate the rules of the Python language. See if you can find and fix them, continuing until you are able to run the file without any error messages. (Create a MS Word file and name it Task2 steps”. Use this word file, to write what you did to fix the errors in this step.)
  3. Once you have eliminated the syntax errors, test the function from the Shell as follows:

>>> mysum(44, 67)

>>> print(mysum(10, 7))

What happens? Why?

(Use the MS Word file “Task2 steps”, and write your answer to this step.)

  1. Fix the function so that both above tests work as expected.  (Use the MS Word file “Task2 steps” and write what you did in this step.)
  2. In hw4Task2.py, write a function named sum_double that accepts two integers as parameters. The function should return the sum of the integers, unless the two values are the same, in which case the function should return double their sum. For example:

                 >>> sum_double(1, 2)

                 3

                 >>> sum_double(3, 2)

                 5

                 >>> sum_double(2, 2)

                 8

Be sure to write a docstring for the function.

After writing the function, test it in two ways:

  • Make function calls from the Shell like the ones shown above.
  • Copy the following code into your file:

def test():

    """ function for testing """

    test1 = sum_double(1, 2)

    print('first test returns', test1)

    # Add more tests below

This code provides the beginnings of a test function, with lines that call the function with a set of inputs and print the return value. It’s worth noting that this function does not need a return statement, because its sole purpose is to make test calls and print their results.

You should:

  • add other test cases to the test function
  • run the Python file
  • call the test function from the Shell (by entering the call test()) and check that you obtain the correct return values for each test case.

(Use the MS Word file “Task2 steps”and write what you did in this step.)

Solutions

Expert Solution

Task2 Steps:

1. Syntax Error: Invalid Syntax

This error can be fixed adding colon at the end of the first line of function defination.

2. IndentationError: expected an indented block

This error can be fixed by giving the tab space before the doc string.

3. Test case 1: mysum(44, 67)

This test case will work correctly and will give the expected output.

4. Test case 2: print(mysum(10, 7))

This test case is returning None along with printing the output. This can be fixed by changing the last line of the function defination from "print(total)" to "return total".

Modified Code

def mysum(x, y):

    """ takes two numbers and returns their sum """

    total = x + y

    return total

hw4Task2.py

sumdouble function

def sum_double(a, b):
    
    """returns the sum if two integers are not same, otherwise returns double of sum"""
    
    #return double of sum if two numbers are same
    if(a == b):
        return 2*(a+b)
    
    #return sum if two numbers are not same
    else:
        return (a+b)

test function

def test():
    
    """function for testing"""
    
    #first test case
    test1 = sum_double(1,2)
    
    #second test case
    test2 = sum_double(3,2)
    
    #third test case
    test3 = sum_double(2,2)
    
    #print the results
    print('first test returns', test1)
    print('second test returns', test2)
    print('third test returns', test3)

function call

test()

Steps:

1. create the function sum_double which takes two parameters as input.

2. check the condition if both the numbers are equal.

3. return double of sum if both the numbers are equal otherwise return sum.

4. create the function test which tests the sum_double function.

5. write 3 function calls to check the input test cases.

6. print the result of the test cases.

7. call the function test to get the result.

All the function has been given with the heading, also comments has been provided for the reference.

Please execute the code in IDE to get the output.


Related Solutions

Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the...
In IDLE - Python 3, do the following: 1. Create File --> New 2. Enter the code below in the new file (you may omit the comments, I included them for explanation #Python Code Begin x = int(input("Enter a number: ")) y = int(input("Enter another number: ")) print ("Values before", "x:", x, "y:", y) #add code to swap variables here #you may not use Python libraries or built in swap functions #you must use only the operators you have learned...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
- Error Checking - Use of Functions - Menu system - Array Processing
C language and that must contain the following: - Error Checking - Use of Functions - Menu system - Array Processing  
Directions Use the Crosstabs option in the Descriptives menu to answer the questions based on the...
Directions Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario. (Be sure to select Chi-square from the Statistics submenu and Observed, Expected, Row, and Column in the Cells submenu. Assume a level of significance of .05). Scenario The school district recently adopted the use of e-textbooks, and the superintendent is interested in determining the level of satisfaction with e-textbooks among students and if there is a relationship between the level of satisfaction...
Use the Chi-Square option in the Nonparametric Tests menu to answer the questions based on the...
Use the Chi-Square option in the Nonparametric Tests menu to answer the questions based on the following scenario. (Assume a level of significance of .05 and use information from the scenario to determine the expected frequencies for each category). Scenario: During the analysis of the district data, it was determined that one high school had substantially higher Graduate Exit Exam scores than the state average and the averages of high schools in the surrounding districts. To better understand possible reasons...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Please use python and run in IDLE Question 1 Write functions that do the following: i)...
Please use python and run in IDLE Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii)...
Directions: Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario.
  Directions: Use the Crosstabs option in the Descriptives menu to answer the questions based on the following scenario. (Be sure to select Chi-square from the Statistics submenu and Observed, Expected, Row, and Column in the Cells submenu. Assume a level of significance of .05) The school district recently adopted the use of e-textbooks, and the superintendent is interested in determining the level of satisfaction with e-textbooks among students and if there is a relationship between the level of satisfaction...
Directions Use the Chi-Square option in the Nonparametric Tests menu to answer the questions based on...
Directions Use the Chi-Square option in the Nonparametric Tests menu to answer the questions based on the following scenario. (Assume a level of significance of .05 and use information from the scenario to determine the expected frequencies for each category). Scenario During the analysis of the district data, it was determined that one high school had substantially higher Graduate Exit Exam scores than the state average and the averages of high schools in the surrounding districts. To better understand possible...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT