#๐ Answer will likely be obvious, but I cant work it out
14 messages ยท Page 1 of 1 (latest)
@sharp warren
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
How do I fix time() not working due to an out of bounds error.
Apologies for the strange screenshot, python was showing my folder name in the shell and I needed to leave that out
you're importing the function time, but then you're also naming a variable time inside your function
use a different name in your function
for this line, use a name other than time
(you're also not timing correctly but one thing at a time)
Python looks ahead in the function definition to determine which variables are local to the function, and which are from outside.
def fun():
print(time)
``` This uses whatever `time` is from outside the function.
```py
def fun2():
time = 2
print(time)
``` This uses the local variable `time` from inside the function, instead.
```py
def fun3():
print(time) # error
time = 2
``` This *also* tries to use the local variable `time` in the function. But that variable is not yet defined when the `print(time)` code attempts to run, so it throws an error.
Best option, as Fashoomp points out: just rename the variable in the function to avoid confusion.
Another tip , perf_counter is more accurate than time
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.