import asyncio from REPL import REPLApp, RunSync, cmd2 class MyApp(REPLApp): @RunSync async def do_guess_number(self, args): """ play guess number game """ import random number = random.randint(1, 100) await self.print("Guess a number between 1 and 100") while True: guess = await self.input("Your guess: ") if not guess.isdigit(): await self.print("Please enter a valid number.") continue guess = int(guess) if guess < number: await self.print("Too low!") elif guess > number: await self.print("Too high!") else: await self.print("Congratulations! You guessed the number.") break async def mainLoop(): app = MyApp() app.preloop() try: stop = False while not stop: line = await app.input(app.prompt) try: stop = app.onecmd_plus_hooks(line) except asyncio.CancelledError: await app.print("^C: Task cancelled") finally: app.postloop() if __name__ == "__main__": asyncio.run(mainLoop())