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:
- Why is
input_check()
being completely ignored? - 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? - 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)