[SOLVED] Python input() not being called during function call

Table of Contents

Issue

This is a code snippet from a simple text based game I’m writing to improve my Python skills.
I plan on using input_check() to simplify a lot of code that I’ll write later on in the project, but I can’t get it to work at the moment. I’m running this on the latest master of VS Code with the Pylance extension, which doesn’t flag any errors in my code. I’ve run multiple tests to make sure input_check() is the issue, and removing it and simply running the code multiple times works just fine.

import time

def rules():
  print("The rules of this game are:")
  time.sleep(0.5)
  print("rules")
  time.sleep(0.5)
  print("rules")
  time.sleep(0.5)
  print("rules")
  time.sleep(0.5)
  print("rules")
  time.sleep(0.5)
  print("rules")
  time.sleep(0.5)
  input_check("Do you understand?\n", rules(), "Ok. Starting game...")
  
def input_check(question: str, function, confirmation: str):  
  yes_no = input(question)

  if yes_no.lower() == "n" or "no":
    function
  elif yes_no.lower() == "y" or "yes":
    print(confirmation)
    time.sleep(1)
  else:
    print("Invalid input.")
    input_check(question, function, confirmation)


input_check("Do you know the rules?\n", rules(), "Ok. Starting game...") 

I’m almost a complete newbie to Python so I have no idea if taking the function argument and then running it as a function later in input_check() would work or not, but that’s not the issue.

There should be a prompt run to define yes_no with input() but it never reaches this. Instead, it seems like it skips ahead to running rules() (which should only happen if the user inputs ‘no’ or ‘n’), and rules() runs continuously until stopped, completely skipping input_check() .

My questions are:

  1. Why is input_check() being completely ignored?
  2. Can you run code taken as a parameter as-is (the function parameter) or is there an extra step I need to make it run?
  3. Is there a better/more efficient way to do this? (e.g. a package that parses input that avoids having to make your own function)

Solution

Look at this statement:

input_check("Do you know the rules?\n", rules(), "Ok. Starting game...")

When you do that, Python is going to CALL the rules function immediately, so it can pass it’s result to input_check. Your rules function prints out a bunch of stuff, then has the exact same line, which is going to call rules() again, and again, and again, and again… It never gets a chance to call input_check. It’s still processing the parameters.

If you want to PASS the function object but not call it, don’t use the parens:

input_check("Do you know the rules?\n", rules, "Ok. Starting game...")

Note that the input_check function will keep calling the passed in function. You DON’T need to call it again inside rules.

Followup

This does not do what you think:

  if yes_no.lower() == "n" or "no":

That’s parsed as:

  if (yes_no.lower() == "n")   or "no":

and since "no" is true, that if will always be taken. You should use one of these:

  if yes_no.lower() in ("n" or "no"):
  if yes_no.lower()[0] == "n":

Next, you have this:

  if yes_no.lower() == "n" or "no":
    function

Here, you DO want to call the function, so you need to add the parens:

  if yes_no.lower()[0] == "n":
    function()

Answered By – Tim Roberts

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.