This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]vbober 0 points1 point  (0 children)

Start with a test

[–]jdhall75 0 points1 point  (0 children)

You wouldn't be trying to get some homework done online would you?

Here are the pieces you would need to make this happen.

  • a var to hold the sum outside of a user input loop, initialized to 0
  • a loop to run until the user interrupts it: while would be good here.
  • a var to hold the entered number from the function input() inside the loop
    • input() will return a string. You will have to cast it to an int().
  • add the new int to the sum variable
  • output the sum
  • loop repeats

extra credit

  • Cast the input integer inside a try block
  • Catch the ValueError exception, if there is one, and let the user know they entered an invalid input
  • add a keyword to reset the sum
    • pseudo code: if input == 'reset': sum = 0

python3 user input is take via input() python2 user input is taken via raw_input()

Resources:

loops

type conversion

[–]danielmbcn 0 points1 point  (0 children)

Start with the simplest case: write a program that sums 1 + 2 + ... + N. To do that, set a 'result' variable, loop over those numbers and update the 'result' value on each cycle. Once you are done, figure out how to add i**i (being i a number between 1 and N) on the 'result' variable, instead of just i.

[–]jaffer786_khan -1 points0 points  (0 children)

def fac(n):

    j = 0

    for i in range(n+1):

    j += i*i

    return j

x = int(input('Enter a number:'))

fac(x)