I wrote a simple code to check if a list is sorted or not. I have two questions: First, my result is wrong. I guess the issue is with following the line. Which one is correct?:
sorted_list = mylist[:].sort()
sorted_list = list(mylist[:]).sort()
As second question, when I try to print(sorted_list), I get NameError which is sorted_list is not defined which is weird. Because I've already defined it in the second line. Would you please help me understand why I'm getting this error?
def is_sorted(mylist):
sorted_list = mylist[:].sort()
# mylist.sort()
if sorted_list == mylist:
return True
else:
return False
print(is_sorted(['Aajid', 'Bafiee', 'Hello']))
print(sorted_list)
Output:
False
Traceback (most recent call last):
File "e:\NectCloud\Python\is_sorted.py", line 11, in <module>
print(sorted_list)
^^^^^^^^^^^
NameError: name 'sorted_list' is not defined
Thanks