Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python input() not being called during function call

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() .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading