Python Forum

Full Version: Writing a function that changes its answer based on user input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The prompt: Write a function that takes three integer parameters: a starting integer, an ending integer, and the string "even" or the string "odd". If oddeven == "even", it will return the sum of all the even numbers from the starting integer to the ending integer (including endpoints). If oddeven == "odd", then it will return the sum of all the odd integers from the starting integer to the ending integer (including endpoints). (Hint: the modulus (%) operator may be very helpful here)

def consecutivesum(start, end, oddeven):
  ototal = 0
  etotal = 0
  #ototal means odd total, etotal means even total
  for i in range(start, end + 1):
    if oddeven == "even":
      if i%2 == 0:
        etotal = etotal + i
  return etotal
    if oddeven == "odd":
      if i%2 == 1:
        ototal = ototal + i
  return ototal

print (consecutivesum(1, 8, "odd"))
Error:
Traceback (most recent call last): File "python", line 31 if oddeven == "odd": ^ IndentationError: unexpected indent
But when I do unindent it, the code becomes "unreachable." I also tried using elif but it returned a syntax error. I think I messed up something conceptually too...

Update: I tried separating the prompt into two separate codes, and it worked. I just don't know how to combine them.

def consecutivesum(start, end, oddeven):
  etotal = 0
  for i in range(start, end + 1):
    if oddeven == "even":
      if i%2 == 0:
        etotal = etotal + i
  return etotal

print (consecutivesum(1, 8, "even"))

def suM(start, end, oddeven):
  total = 0
  for i in range(start, end + 1):
    if oddeven == "odd":
      if i%2 == 1:
        total = total + i
  return total

print (suM(3, 7, "odd"))
BTW I'm using the repl.it IDE
With this bit of code:

def consecutivesum(start, end, oddeven):
  ototal = 0
  etotal = 0
  #ototal means odd total, etotal means even total
  for i in range(start, end + 1):
    if oddeven == "even":
      if i%2 == 0:
        etotal = etotal + i
  return etotal
    if oddeven == "odd":
      if i%2 == 1:
        ototal = ototal + i
  return ototal
 
print (consecutivesum(1, 8, "odd"))
Your return is the problem. You can't indent after a return. Since the return is unindented so far, it is not part of the for loop. So the for loop runs, but only calculates etotal, which it then returns (if the indentation problem was fixed). So if oddeven is 'odd', it always returns 0.

I would only have one total variable, and one return statement at the very end of the function. Then add to it based on oddeven and the mod of i.

Also note that you can simplify your conditionals. This:

    if oddeven == "even":
      if i%2 == 0:
        etotal = etotal + i
is equivalent to this:

    if oddeven == "even" and i%2 == 0:
      etotal = etotal + i
The issue is that you need to adjust the start position.
If start is even and you need to count odds, then advance start to the first odd in range.
The opposite is true as well.
also you need to include endpoints so end will need to be bumped up on so it is within range.
After you do that you can have a single result and use a range that counts by 2's while adding each number to result.
Then a single return statement.
Give it another shot and post results.