Posts: 9
Threads: 5
Joined: Sep 2017
Sep-20-2017, 06:12 PM
(This post was last modified: Sep-20-2017, 07:58 PM by sparkz_alot.)
# 3. Using the following list and a “for” loop, display differences of all consecutive pairs of numbers in the list.
our_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12]
for i in our_list:
print(i-1)
Posts: 3,458
Threads: 101
Joined: Sep 2016
Did you run it? Does the output seem right?
Posts: 9
Threads: 5
Joined: Sep 2017
Sep-20-2017, 07:18 PM
(This post was last modified: Sep-20-2017, 09:00 PM by Larz60+.)
output looks like this but I want it to subtract consecutive pairs of numbers:
our_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12]
for i in our_list:
print(i-1) Output: 0
1
4
5
2
76
8
-1
2
22
-0.6
-13.4
-4.12
Posts: 3,458
Threads: 101
Joined: Sep 2016
If you have an element, and subtract 1 from it, how would that be related to finding the difference between two different elements?
Try this: our_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12]
for ndx in range(0, len(our_list), 2):
left = our_list[ndx]
right = our_list[ndx+1]
print("{0} - {1} = {2}".format(left, right, (left-right)))
Posts: 9
Threads: 5
Joined: Sep 2017
pour_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12]
for i in range(0, len(our_list), 2):
left = our_list[i]
right = our_list[i+1]
print("{0} - {1} = {2}".format(left, right, (left-right)))
1 - 2 = -1
5 - 6 = -1
3 - 77 = -74
9 - 0 = 9
3 - 23 = -20
0.4 - -12.4 = 12.8
Traceback (most recent call last):
File "<ipython-input-5-3f1c5e913d44>", line 5, in <module>
right = our_list[i+1]
IndexError: list index out of range
why does it keep saying list index out of range?
Posts: 7,320
Threads: 123
Joined: Sep 2016
Sep-22-2017, 08:28 PM
(This post was last modified: Sep-22-2017, 08:28 PM by snippsat.)
You have been warned 2-3 times before about not using code tag and indentation.
Read BBCode help.
Short version use Ctrl+Shif+v when copy in code to keep indentation.
Mark all code and push button
Then code look like this.
class Parent:
def my_method(self):
print('Calling parent method')
class Child(Parent):
def my_method(self):
print('Calling child method')
super().my_method()
c = Child()
c.my_method() Try to follow this,if not your next post will be deleted until okay.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Threads merged. This isn't a separate issue, so no need for another thread.
...especially when your code is literally just my code, with the error I purposefully left in there, to help you think about what you're doing ;)
|