No working code?

Code dont work any idea why?
Repl link:

import pygame
import sys
import importlib.util
import threading
from io import StringIO
import sys as stdsys

# Initialize Pygame

# Set up the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("User Input and Console Output")

# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up fonts
font = pygame.font.SysFont(None, 36)

# Function to run your main file
def run_main_file():
    main_spec = importlib.util.spec_from_file_location("main", "")
    main_module = importlib.util.module_from_spec(main_spec)

    # Redirect the standard output to capture console output
    old_stdout = stdsys.stdout
    stdsys.stdout = mystdout = StringIO()

    except Exception as e:

    # Restore the standard output
    stdsys.stdout = old_stdout

    return mystdout.getvalue()

# Create a thread to run the main file
main_thread = threading.Thread(target=run_main_file)

# Main loop
running = True
user_input = ""
console_output = "Welcome to Pygame!"

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                console_output = user_input
                user_input = ""
            elif event.key == pygame.K_BACKSPACE:
                user_input = user_input[:-1]
                user_input += event.unicode

    # Clear the screen

    # Get console output from the main file
    console_output = run_main_file()

    # Render text
    input_text = font.render("Input: " + user_input, True, BLACK)
    output_text = font.render("Output: " + console_output, True, BLACK)

    # Draw text on the screen
    screen.blit(input_text, (50, 50))
    screen.blit(output_text, (50, 100))

    # Update the display

# Quit Pygame

Please provide error output/description and perhaps some screenshots so we understand what exactly needs debugging or improvement.


this block of code makes no sense, and you should probably delete it.

it was for running my main file

That is the output tab, which is capable of showing graphical/pixel output, not the console tab, which show the various logs from your python program.

ok well i just give up


Please don’t give up! Failure is a major part of programming, for without it we’ll never improve.


i dont give up on coding i give up on displaying it

Please just clarify this one thing for me: What type of content are ou trying to display?

the console. I dont want to share replit due to some “issues” so I was trying to make a dev link in HTML so they can play it without editing the code to cheat.

Replit does not provide such functionality unfotunately, but you might want to look into using pyscript

whats that? Is it usable on replit?

You can’t share the native console (not usable), but you can try to use pyscript to make a website which runs python.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.