Last answered:

20 Jan 2022

Posted on:

20 Jan 2022

0

Code not working as shown in the video

Hello, I followed the above lesson and typed my code exactly as in the video but my code is having some error shown below when I try to run it. What can be the problem?

data=[53,76,25,98,56,42,69,81]
data_copy=data[:]
for i in range(len(data_copy)):
    for j in range(0,len(data_copy)-i-1):
        if data_copy[j] > data_copy[j+1]:
            data_copy[j],data_copy[j+1]=data_copy[j+1],data_copy
print(data_copy)

runfile('C:/Users/localadmin/.spyder-py3/untitled4.py', wdir='C:/Users/localadmin/.spyder-py3')
Traceback (most recent call last):

File "C:\Users\localadmin\.spyder-py3\untitled4.py", line 20, in <module>
    if data_copy[j] > data_copy[j+1]:

TypeError: '>' not supported between instances of 'list' and 'int'

1 answers ( 0 marked as helpful)
Instructor
Posted on:

20 Jan 2022

0

Hey Jared,

Thank you for your question!

The problem is in the second to last line. The line should read:

data_copy[j],data_copy[j+1]=data_copy[j+1],data_copy[j]

where I have added [j] at the very end.

Unfortunately, in the video, the console panel is hiding the end of that line, so the [j] is not visible at all times. I would suggest downloading the resources for the course. The relevant code you can find in the 6_1_loops.py file.

Hope this helps!

Kind regards,
365 Hristina

Submit an answer