Question

In: Computer Science

a_list = [1, 2, 3, 4, 5] list_index = 7 #----------------------------------------------------------- #You may modify the lines...

a_list = [1, 2, 3, 4, 5]
list_index = 7

#-----------------------------------------------------------
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.

#In this problem, we're going to use some unfamiliar syntax.
#You'll learn more about this syntax in Unit 4. For now,
#though, you don't need to understand the syntax. All you
#need to know is that right now, this code will cause an
#error with the values supplied above.
#
#Revise this code so that this error is caught, and the
#message "Invalid index!!" is printed. However, your revision
#must meet these requirements, too:
#
# - If the values of the variables above are changed so
# that the error doesn't occur, the program should behave
# the same way that it does now.
# - The two first and last lines, "Accessing index..." and
# "Done!", should print whether or not an error occurs.
# - If a *different* error occurs from the one that arises
# initially, your code should instead print "Unknown
# error!"
#
#HINT: You won't be able to do this without running the code
#first and seeing what the error is. So, try it out first!

#Revise this code:
print("Accessing index...")
result = a_list[list_index]
print("Value at index:", result)
print("Done!")

Solutions

Expert Solution

a_list = [1, 2, 3, 4, 5]
list_index = 7

# -----------------------------------------------------------
# You may modify the lines of code above, but don't move them!
# When you Submit your code, we'll change these lines to
# assign different values to the variables.

# In this problem, we're going to use some unfamiliar syntax.
# You'll learn more about this syntax in Unit 4. For now,
# though, you don't need to understand the syntax. All you
# need to know is that right now, this code will cause an
# error with the values supplied above.
#
# Revise this code so that this error is caught, and the
# message "Invalid index!!" is printed. However, your revision
# must meet these requirements, too:
#
# - If the values of the variables above are changed so
# that the error doesn't occur, the program should behave
# the same way that it does now.
# - The two first and last lines, "Accessing index..." and
# "Done!", should print whether or not an error occurs.
# - If a *different* error occurs from the one that arises
# initially, your code should instead print "Unknown
# error!"
#
# HINT: You won't be able to do this without running the code
# first and seeing what the error is. So, try it out first!


# Revise this code:
print("Accessing index...")
try:
    result = a_list[list_index]
    print("Value at index:", result)
except IndexError:
    print("Invalid index!!")
except:
    print("Unknown error!")
print("Done!")


Related Solutions

4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1,...
4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1, 7}. Answer the following questions, giving reasons for your answers. a) Is ? ⊆ ?? b)Is ? ⊆ ?? c) Is ? ⊂ ?? d) Is ? ⊆ ?? e) Is ? ⊆ ?? 5) Let ? = {1, 3, 4} and ? = {2, 3, 6}. Use set-roster notation to write each of the following sets, and indicate the number of elements in...
Maria says that 4 2/3 x 7 1/5 = 4 x 7 + 2/3 x 1/5....
Maria says that 4 2/3 x 7 1/5 = 4 x 7 + 2/3 x 1/5. Explain why Maria has made a good attempt, but her answer is not correct. Explain how to work with what Maria has already written and modify it to get the correct answer. In other words, don’t just start from scratch and show Maria how to do the problem, but rather take what she has already written, use it, and make it mathematically correct. Which...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5 8 7 6 5 7 7 6 7 8 8 8 9 7 8 10 12 11 Test the significance of the correlation coefficient. Then use math test scores (X) to predict physics test scores (Y).  Do the following: Create a scatterplot of X and Y. Write the regression equation and interpret the regression coefficients (i.e., intercept and slope). Predict the physics score for each....
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
Step 2 Data Set A x 1 2 3 4 5 6 7 y 7 7...
Step 2 Data Set A x 1 2 3 4 5 6 7 y 7 7 7 9 9 9 10 Data Set B x 1 2 3 4 5 6 7 8 9 10 11 y 4 6 6 6 8 9 9 9 10 10 10 Step 2 Find the equation for the least-squares line, and graph the line on the scatter plot. Find the sample correlation coefficient r and the coefficient of determination r2. Is r significant?...
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1...
Given: x y -5 1 -4 5 -3 4 -2 7 -1 10 0 8 1 9 2 13 3 14 4 13 5 18 What are the confidence limits (alpha = 0.05) for the true mean value of Y when X = 3?
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3....
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3. 9.   3.      4. 5.   6.      5. 7.   2.      6. 6.   4.   Test of hypothesis for difference of two means by using 1. t Test 2. ANOVA Take 5% type 1 error, then compare the results. Show that t² is equal to f.
Consider the data. xi 1 2 3 4 5 yi 4 7 5 11 15 The...
Consider the data. xi 1 2 3 4 5 yi 4 7 5 11 15 The estimated regression equation for these data is  ŷ = 0.60 + 2.60x. (a)Compute SSE, SST, and SSR using equations SSE = Σ(yi − ŷi)2, SST = Σ(yi − y)2, and SSR = Σ(ŷi − y)2. SSE=SST=SSR= (b) Compute the coefficient of determination r2. r2 = Comment on the goodness of fit. (For purposes of this exercise, consider a proportion large if it is at least...
Consider the data. xi 1 2 3 4 5 yi 4 7 5 11 13 (a)...
Consider the data. xi 1 2 3 4 5 yi 4 7 5 11 13 (a) Compute the mean square error using equation  s2 = MSE = SSE n − 2  . (Round your answer to two decimal places.) (b) Compute the standard error of the estimate using equation s = MSE = SSE n − 2  . (Round your answer to three decimal places.) (c) Compute the estimated standard deviation of b1 using equation sb1 = s Σ(xi − x)2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT