I’m making a choose-your-own adventure program for my AP CSP class. I have enclosed screenshots below. I defined a function that has an input within it (that is the screenshot that starts with def everyone_starts_here()), and then I called the function (that is the screenshot that starts with everyone_starts_here()), but when I tried to run the program it gave me an error that said ‘await’ outside async function (that’s the other screenshot). What am I doing wrong?
Asyncio is a Python standard library module which allows code to run such that you don’t care the order in which things happen. You use async def to say a function is asynchronous, and then you await the result of async functions, which waits for them to return a result before continuing on. time.sleep is synchronous, as execution waits for the time to elapse before continuing; asyncio.sleep is asynchronous, as it calls a function which can run independently of the main flow, then awaits the result of that process before continuing.
Someone smarter than me can probably tighten up that explanation…
Importantly, though, is that synchronous code can’t call async. So, if time.sleep were actually async.sleep, you’d get that await error.
input is synchronous, and there’s no obvious analog in the async library you could’ve accidentally imported, so I suspect you’re running the wrong file.
Not a SyntaxError. It would just return a coroutine, which might produce a RuntimeWarning at worst.
Line 15 of main.py must have had an await. I don’t think we’ve been shown the code that is being run. As @JanEricNitschke said, we need to see the full code and how it is being invoked.
Uhhh…I have 128 lines of code, but sure. I am running it in the sandbox of codeHS since this is what my teacher has us use. It’s supposed to be a choose-your-own adventure game
Text is WAY easier than screenshots, for future reference But, this code clearly isn’t where that error came from. There is no await anywhere that I can see.
No probs. When I make comments like that text is easier, I’m not whining that it’s too hard to help you, I’m offering suggestions for the future I want you to succeed, and on a personal level, I don’t like unsolved mysteries!
When you showed the error, you showed one small part of it. There is often a full traceback showing multiple file and line numbers. Can you show the entire traceback? That might give us a clue. (Notably, it’s likely that main.py is a driver that calls your code, so line 15 of that is just going to be something that says “hey go do the thing”.)
Sorry I took so long to reply. The screenshot that I sent of the error message was the whole thing. Should I try copy and pasting the whole program into something else and then seeing if it works then?