#๐ i am trying to find the sum of 2nd even and 2nd last even number of a list
45 messages ยท Page 1 of 1 (latest)
@old fable
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
if the first is even this wont work
can you explain?
why 5
according to the title aint this supposed to reslt in 14?
oh no sorry just misread an i for a 1
lol alr
the second even is 4 the second to last even is 10 so the sum is 14 aint it?
!code
so i can run it?
c=0;h=0;twe=0;two=0
for i in range(len(a)):#range
if a[i] % 2 == 0:#check if even
c+=1;#count of even
if c==2:
two=a[i]
#2nd even number
for k in range(i+1,-1):
if a[k] % 2==0:
h+=1
if h==1:
twe=a[i]
print(two,twe)
```
Hey @old fable!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
a=[1,2,3,4,5,6,7,8,9,10,11,12]
c=0;h=0;twe=0;two=0
for i in range(len(a)):#range
if a[i] % 2 == 0:#check if even
c+=1;#count of even
if c==2:
two=a[i]
#2nd even number
for k in range(i+1,-1):
if a[k] % 2==0:
h+=1
if h==1:
twe=a[i]
print(two,twe)
```
i did a little change and its hsowing 4
but is the 2nd last even logic correct?
I think a better aproach would be to start reading the list from the front store the second even and break
Than do the same from the end
I don't think so there are simpler ways for sure
so ill check for the 2nd last even from the end?
okay
Looks like the second if behaved like an elif for no reason
Yes iterate from the end of the list backwoards
ohh
i need to make another for loop
thank you!!
c=0
for i in list:
if i%2==0:
if c ==1:
a=i
break
c=1
Than the same with the list reversed
somehow still worked ๐คฃ
Also add a break to the second too
Its the same with a litle order change
And bc u dont use i for else than indexing u could iter the list and
list[::-1]
The second is same as reversed(list)
i havent got to that part yet ig
oh yeah
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.