In: Computer Science
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.
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))