Python Forum
Writing a function that changes its answer based on user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing a function that changes its answer based on user input
#1
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
Reply
#2
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Answer for newbie, Sqlite3 Froger 2 844 Sep-27-2023, 05:33 PM
Last Post: noisefloor
  Coding answer for newbie cg3 2 1,130 Aug-05-2023, 12:17 PM
Last Post: jefsummers
  How to create a menu and execute a function based on user selection in python? Thedarkphoenix 1 1,312 Nov-23-2022, 07:53 PM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,280 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Input function Fabio87 11 5,926 Apr-29-2022, 03:50 PM
Last Post: Alex143
  Print user input into triangle djtjhokie 1 2,361 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,962 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  sys.stdin to do a word count based on user entry Kaltex 3 3,666 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  What am I doing wrong to not get the answer pav1983 7 3,668 Jun-25-2020, 08:53 PM
Last Post: ndc85430
  how to add the user input from file into list wilson20 8 4,298 May-03-2020, 10:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020