Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:25:33+00:00 2026-06-15T18:25:33+00:00

This is my code: def sum_even(a, b): count = 0 for i in range(a,

  • 0

This is my code:

def sum_even(a, b):
    count = 0
    for i in range(a, b, 1):
        if(i % 2 == 0):
            count += [i]
        return count

An example I put was print(sum_even(3,7)) and the output is 0. I cannot figure out what is wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:25:34+00:00Added an answer on June 15, 2026 at 6:25 pm

    Your indentation is off, it should be:

    def sum_even(a, b):
        count = 0
        for i in range(a, b, 1):
            if(i % 2 == 0):
                count += i
        return count
    

    so that return count doesn’t get scoped to your for loop (in which case it would return on the 1st iteration, causing it to return 0)

    (And change [i] to i)


    NOTE: another problem – you should be careful about using range:

    >>> range(3,7)
    [3, 4, 5, 6]
    

    so if you were to do calls to:

    • sum_even(3,7)
    • sum_even(3,8)

    right now, they would both output 10, which is incorrect for sum of even integers between 3 and 8, inclusive.

    What you really want is probably this instead:

    def sum_even(a, b):
        return sum(i for i in range(a, b + 1) if i % 2 == 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code: def some_method(): #i need to get some attrs :( return
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have this code: def __init__(self, a, b, c, d...): self.a = a self.b
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I've got this code: def __parse(self): for line in self.lines: r = Record(line) self.records[len(self.records):]
Please look at this code: def chop(array, search): lo = 0 high = len(array)
I have a app/helpers/application_helper.rb file with this code: def clippy(text, bgcolor = #ffffff) text.gsub!('',')
I thought that I improve performance when I replace this code: def f(a, b):
this is the code: def create(request, form_class=MapForm, template_name=maps/create.html): map_form = form_class(request.POST or None) if
I have this code: import re def doReplace(toReplace): i = 1 def chapterReplacer(_): result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.