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 8563693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:01:09+00:00 2026-06-11T17:01:09+00:00

I am working on a function which takes a positive integer N and computes

  • 0

I am working on a function which takes a positive integer N and computes the following sum:

1 - 2 + 3 - 4 + 5 - 6 + .... N. 

My professor said we can not use “math formulas” on this, so how could I go about it?

I thought about using a for loop, but I don’t know how or what to write for the print statement. This is what I have so far:

n=int(input("enter N="))
for i in range(1, n+1, 1):
    if i % 2 == 0:
        # I'm not sure what print statement to write here

I tried print(i= "+" i+1) and just weird stuff like that, but I get errors and am lost after that.

what i have now

n=int(input('enter N='))
total=0
print("sum",)
for i in range(1, n+1, 1):
    if i % 2 == 0:
        count=count - i
        print("-", i)
    else:
        count=count + i
        print("+", i, end=" ")
print("=", total)

#still get errors saying name count not defined and unsupported sperand for +:   
  • 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-11T17:01:10+00:00Added an answer on June 11, 2026 at 5:01 pm

    First, to accumulate the sum as described, and to do it by looping instead of “using math formulas”, you want to do this:

    n=int(input("enter N="))
    total = 0
    for i in range(1, n+1, 1):
        if i % 2 == 0:
            ????
        else:
            ????
    print(total)
    

    You should be able to figure out what the ???? are: you need to do something to total for each i, and it’s different for even and odd.

    Now, if you want it to print out something like this:

    sum 1 – 2 + 3 – 4 + 5 – 6 + …. N = X

    You can print things out along the way like this:

    n=int(input("enter N="))
    total = 0
    print ("sum", end=" ")
    for i in range(1, n+1, 1):
        if i % 2 == 0:
            print("-", i, end=" ")
            ????
        else:
            if i == 1:
               print(i, end=" ")
            else:
               print("+", i, end=" ")
            ????
    print("=", total)
    

    If you want to keep negative and positive totals separately and then add them, as you mentioned in an edit, change total = 0 to ntotal, ptotal = 0, 0, then change each of the ???? parts so one works on one total, one on the other.

    All of the above is for Python 3. If you want to run it in Python 2, you need to turn those print function calls into print statements. For the simple cases where we use the default newline ending, that’s just a matter of removing the parentheses. But when we’re using the end=" ", Python 2’s print statement doesn’t take keyword parameters like that, so you instead have to use the “magic comma”, which means to end the printout with a space instead of a newline. So, for example, instead of this:

    print("+", i, end=" ")
    

    You’d do this:

    print "+", i,
    

    Again, for Python 3, you don’t have to worry about that. (Also, there are ways to make the exact same code run in both Python 2 and 3—e.g., use sys.stdout.write—which you may want to read up on if you’re using both languages frequently.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project using OpenCV, and there is a function which takes
I am currently working on a function in which I use a recursive CTE,
I'm working on a javascript function which takes in the names of three controls,
I am working with a javascript function which takes a value from a <input
I have a function which can vary in the time it takes to finish.
I am working with Silverlight Unit Testing i need to test a function which
I have created a add input field function which is working fine. I would
I have a div show hide function which is working fine. when I click
I am working on a function that will essentially see which of two ints
I am working on a code which requires a function. This function gets a

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.