Python Forum

Full Version: Function question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, for my homework assignment, I am required to write a function called "check_answer" which will determine if a given addition or subtraction problem was solved correctly.

I would like to know how I can properly call this function, and how I can start checking the answer. Am I on the right track by using "if statements?"

(Please excuse my newbie language, I'm really new to python as you can tell.)



 

def Main():
  int1 = int(input("Please enter a integer: "))
  int2 = int(input("Please enter a second integer:"))
  return int1
  return int2


def check_answer():

  print("Started")

  answer1 = check_answer(1, 2, 3, "+")
  output = (answer1)
  print(answer1)
  if answer1 == 6:
    print ("True")
  
  if answer1 != 6:
    print("False")

  answer2 = check_answer(1, 2, -1, "-")
  output = (answer2)
  print (answer2)

  if answer2 == -1:
    print("True")

  if answer2 != -1:
    print("False")
  
  answer3 = check_answer(9, 5, 3, "+")
  output = (answer3)
  print (answer3)

  if answer3 == 17:
    print("True")

  if answer3 != 17:
    print("False")

  answer4 = check_answer(8, 2, 4, "-")
  output = (answer4)
  print (answer4)

  if answer4 == 4:
    print("True")

  if answer4 != 4:
    print("False")

  print(check_answer)
  return output

if __name__ == "_check_answer_":
  check_answer()
First, you can't use multiple return statements the way you are trying to. As soon as a return statement is processed, the function stops processing code. If you need to return two values, return them as a tuple:

def Main():
  int1 = int(input("Please enter a integer: "))
  int2 = int(input("Please enter a second integer:"))
  return int1, int2
Although you never call main, so I guess that's not a problem?

You define check_answer with no parameters, but then you are calling it with four parameters: answer1 = check_answer(1, 2, 3, "+"). And even if the parameters are straightened out, that's not going to work. You'll call check_answer, it will get to line 11 and call check_answer again, which will get to line 11 and call check_answer again, which will get to line 11 and call check_answer again, and so on until you get a recursion error.

I think you want to rename what you have as check_answer as check_all, and then write another function check_answer that actually does the check.

You keep storing new answers to output, but that over writes the old ones. So at the end, you are just returning the last answer. Maybe you want that to be a list of answers, which you append each answer to?

You do this alot:

  if answer2 == -1:
    print("True")
 
  if answer2 != -1:
    print("False")
Since the second test is a contradiction of the first test, you can just use an else statement:

  if answer2 == -1:
    print("True")
 
  else:
    print("False")
Thank you, with much trial and error I figured out what the additional issues were within my code.