- 1
Python debugging, but nothing come out if the code is running
Assume that you have written the following code to find the middle element from a 3-element list. def findmiddle(a): if ((a[0] >= a[1]) and (a[1] >= a[2])) or ((a[0] <= a[1]) and (a[1] <= a[2])): return a[1] elif ((a[0] >= a[2]) and (a[2] >= a[1])) or ((a[0] <=a[2]) and (a[2] <= a[1])): return a[2] else: return a[0] Notice that if a list is passed in that is not of length at least 3, the code will give an error. Modify the function so that it will raise an exception if the list is not valid. Then, show how you could call the function, printing a message if there was an exception. can you tell how can i raise excempetion if there's nothing come out if I run this code
2 ответов
+ 6
Maple Tree ,
is this your complete code? what you are showing is only the function, but it is never called. to get a result you need this:
def findmiddle(a):
.....
print(findmiddle([2,1,3])) #<<<<< this function call is missing in your code
i would recommend you to start learning from one of the python tutorials.
+ 1
Exceptions will be explained in the Python Intermediate course, keep learning!
Also the indentation in your code code messed up. Rather put it in a code script on sololearn playground and share the link.
With middle element– do you mean the element that is smaller than the max and greater than the min? What if there are duplicate values in the list?