#๐Ÿ”’ 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)

sly turtleBOT
#

@old fable

Python help channel opened

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.

old fable
#

its showing 5 instead of 4*

magic salmon
#

if the first is even this wont work

old fable
#

can you explain?

magic salmon
#

why 5
according to the title aint this supposed to reslt in 14?

magic salmon
old fable
magic salmon
#

the second even is 4 the second to last even is 10 so the sum is 14 aint it?

old fable
#

it is but how is it showing 5 instead of 4?

#

its shwoing 5 and 0 ๐Ÿ˜ข

magic salmon
#

!code

sly turtleBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

magic salmon
#

so i can run it?

old fable
#
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)
         ```
sly turtleBOT
#

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.
old fable
#
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?

magic salmon
#

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

magic salmon
old fable
#

so ill check for the 2nd last even from the end?

magic salmon
magic salmon
old fable
#

i need to make another for loop

#

thank you!!

magic salmon
#
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

magic salmon
old fable
magic salmon
#

Also add a break to the second too

magic salmon
old fable
#

okay

#

thank you so much!

magic salmon
#

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)

old fable
old fable
sly turtleBOT
#
Python help channel closed

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.