Question

In: Computer Science

Create a function named ‘dividing’. This function will take one parameter value, which will be an...

Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of the except ZeroDivisionError statement.

In a separate script create a new function named ‘indexvalue’. This function will take one parameter value, which will be an integer.

Within your function you will have the following list:

locations = "Benson", "Douglas", "Sierra Vista", “Online”

For the function you will use ‘Exception Handling’ with the use of the try and except statements. You will be taking input from a user in which you will be asking them to enter an index value to fetch within your list. If the user enters an index value that exists within the list, your function should return that value from your list with use of the index value, not an if statement! However, if the user enters an index value that would not exist within the list, your function should return the following, ‘There is no index value for that number within the list’ through the use of the except IndexError statement.

In a third script create a function named ‘dictionarysearch’ that will take one parameter value. Your function will have a dictionary that contains first the number value of the chapter for our book for chapters 1-5, then include the name for the chapter. For example; {‘1’:‘Python Basics’, ‘2’:‘Flow Control’, etc…….} Next you will ask the user to enter a number between 1 and 5 to display the Chapter names, or they can enter the value of zero to review all values in the dictionary. This value will then be passed to the function you created. If the user inputs a number between 1 and 5 you will need to return the value for the key using a method to retrieve the value from the dictionary, you should not have a list of if statements to display your keys and values! If the user inputs a 0, you will need to use a for loop to return all keys and values from the dictionary. If the user does not enter a 0, or a number between 1 and 5, your function should return the fact that they entered an invalid number to the user.

Solutions

Expert Solution

Question 1

Screenshot

Program

#Function diving take 1 parameter
#Return a value divided by passed parameter
#If passed parameter in 0 generate exception
def divide(divisor):
    try:
        return 10/divisor
    except ZeroDivisionError:
        return 'You tried to divide by zero!'
#Test
print("Test1: ",divide(0))
print("Test2: ",divide(2))

----------------------------------------------------------------------------------------------

Question 2

Screenshot

Program

#Function to get the element at the specified index of a list
#If the index within list return element
#Otherwise generate exception
def indexvalue(index):
    locations = ["Benson", "Douglas", "Sierra Vista", "Online"]
    try:
        return locations[index]
    except IndexError:
        return 'There is no index value for that number within the list'
#Test
index1=int(input("Enter an index value to fetch within your list: "))
print("Test1: ",indexvalue(index1))
index2=int(input("Enter an index value to fetch within your list: "))
print("Test2: ",indexvalue(index2))

-------------------------------------------------------------------------------------------------

Question 3

Screenshot

Program

#function to search in dictionary
#Take a parameter as input
#If it is 0 print return whole dictionary
#If it is any of the key then return corresponding value
#Otherwise generate error
def dictionarysearch(key):
    dictionary= {1:'Python Basics', 2:'Flow Control',3:'Functions',4:'Iterations',5:'Strings' }
    if key==0:
        string="{"
        for key,value in dictionary.items():
            string+=str(key)+':'+value+","
        string=string.rstrip(',')
        string+="}"
        return string
    elif key>=1 and key<=5:
        return dictionary[key]
    else:
        return 'Entered an invalid number!!!'
      
#Test
key1=int(input("Enter a number between 1-5: "))
print('Test1: ',dictionarysearch(key1))
key2=int(input("Enter a number between 1-5: "))
print('Test2: ',dictionarysearch(key2))
key3=int(input("Enter a number between 1-5: "))
print('Test3: ',dictionarysearch(key3))


Related Solutions

Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Design and implement a function which has one input parameter which is a number which is...
Design and implement a function which has one input parameter which is a number which is greater than 50, called num. Then the function will create a dictionary whose keys are 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9. Then the function calculates the values for each of the above keys. The value for a key is all the numbers between 2 and input “num” that are divisible by the key. The function...
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
Identify the steps you would take to create a one-field table named Titles that could
Identify the steps you would take to create a one-field table named Titles that could be used to constrain the values in the Title field of the Employees table to the existing values in that field.
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Write a function named cup that uses a turtle parameter t to draw a cup consisting...
Write a function named cup that uses a turtle parameter t to draw a cup consisting of three sides of a square. Your function should not change the position or orientation of t before drawing the first side of the cup. The turtle t that is passed to cup may initially be either up or down and may be at any location on the screen and in any orientation. After drawing a side, the turtle t should turn left. When...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT