Hi, im kinda new to coding and python, and im just shitting around with some random code to get more advanced but im stuck on one thing, how do make a variable with an input as value(kinda like this:
input1 = input(“please input”) ) without making the input displaying immediately. I also cant put it in a function code block since i need it to have a global scope. Does anyone know how to do it? Tnx
If you collect user input and assign it to a variable, the only one to see it is the user as they type it in and enter it. Now it is memory with a variable to reference it. We can poll the variable any time we like. It doesn’t have to be immediately after it is inputted.
We may need some extra clarification with your question if the answer above is missing the point.
Meaning, you want some kind of delay before the question is seen? Sounds like you’re after sleep.
e.g.
import time
print("hold on")
time.sleep(3)
input1 = input("please input")
Sorry, not entirely following this. First, don’t have stuff in global scope. However, if you must, your function can return data into that scope:
e.g.
import time
def slow_input(msg, delay = 1):
time.sleep(delay)
return input(msg)
print("hold on")
input1 = slow_input("please input")
# input1 now in global scope
I see indeed my question is not really clear, so, what i mean is that if i create a variable wirh an input vue, (like so: input1 = input(“please input”) ) it instantly asks for an input it, what i want is that it doens immediatly ask for that and i can let it ask for an input whenever i need to.
For that you might best use a function that you can call whenever you want user input, based on what the program is doing, or where it is in the program, or based upon some condition, like in an if statement. Still, I would write the input into a function so you only need to write that code once, and can give it parameters.
Hi, your awnser to my problem(ty very much for that btw) is a sortof yes and no solution, yes because i indeed need it to wait a bit and no because i need the input to run at a specific place in the code (a diffrent time then when i need to create it) not after a certain amount of time.
So i tried it, it worked for the first part but then i cant reach the variable of the input in a while loop.
Please show us your code so we can better determine what is happening in real time.
So the basic example of my code currently is:
def example_def():
input1 = input(“run code?”)
run_code = True
while run_code == True:
example_def()
if input1 == “stop code”:
run_code = False
(With the indents ofc)
If i play the code and input stop code(making it: “stop code”) it gives an error that input1 is undifined.
That is a recursive function. These can be tricky to work with and require extreme care in the design else they can run away from you. I’m not too keen on the idea of setting run_code
inside the function. Perhaps set it before the function call.
I’m still not sure what the overall purpose of that code is. Perhaps you can describe the scenario in which it will be implemented?
This is what you’ve offered:
def example_def():
# input function is called, result stored to variable input1
input1 = input("run code?")
run_code = True
while run_code == True:
# whoa, infinite loop time!
example_def()
# the rest doesn't really matter, as you'll never get past the above
# the value of input1, which has been set
# since before we entered the loop, is checked.
if input1 == "stop code":
run_code = False
To illustrate the problem…
def example_def(call_num = 0):
print("call", call_num)
# don't even need this
# while True:
example_def(call_num + 1)
print("this will never print")
example_def()
Try it. It will go boom with something like RecursionError: maximum recursion depth exceeded while calling a Python object
. If you don’t know what recursion is… should probably wait until you get to that bit. What you need to know now is that code that calls itself will be code that calls itself will be code that calls itself … basically, without a coded stop sign, it can never leave. So, don’t do that.
Perhaps you want:
def example_def():
while run_code == True:
# do something else, don't call yourself
# ask the question here
input1 = input("run code?")
if input1 == "stop code":
run_code = False
Okay, thanks, ive also fixed the problem.
I just took EVERYTHING out of a function(the function that had this and a bunch extra) and put it in the while loop, works but is VERY messy.
It took me a while to understand your code because it has no indentation. But anyway i think this is what you meant
so if you would like to make your code more cleaner i guess you could do this