Code file running problem

whenever i press run it runs only 1 file even if i switch it i delete it it gives me “no such file:___”

This is intended. To chnage the file that is ran edit the entry point in the .replt file

1 Like

can you elaborate? i didnt understand

Go to the repl. Then in the file explorer go to the 3 dots and press show hiddent files. Then open the .replit file. Once you do that find the entry point in the file. Then change the file name there.

2 Likes

still the same error but now its a different name, also i have to do this for every single file i want to run?

I need to save this somewhere as I find myself typing it a lot. Editing the hidden files is a totally viable option. However, if you have multiple Py files and don’t wish to manually change stuff every time you want to use one, make the main file (main.py) contain a code similar to this:

choice = int(input("Which file to run?\n1) otherFile\n2) secondFile"))
# Get user input
if choice == 1: # If user chooses option 1
  from otherFile import main # Import the function "main" from
  # "otherFile.py" in the same folder
  main() # This executes the "main" function in "otherFile.py"
elif choice == 2:
  from secondFile import main
  main()
else:
  print("Your selection is invalid. Have a nice day.") # Change this to
  # whatever you want

You can also make this into a loop and do various things to make the selection fancier, but this is just a basic example.

1 Like

how do i use it? it keeps giving me an error

Please provide the link to your Repl and I will look into it.
EDIT: It must be this one as you only have 3 Repls.

1 Like

you need me to upload my repl on there?

You didn’t change the filenames. I said that import otherFile imported from otherFile.py. You need to put in the proper file name and function name for your situation. So, substitute the name of your other Py files in for otherFile. Also you should make all those Py files have a main() function so that you can easily run them. I made a fork here that I will begin the process (although you have lots of files so I won’t do it for all of them).

1 Like

thank you so much your a life saver can you do 1 and i copy the rest?

Copy whatever you want. And you’re welcome! :slightly_smiling_face:

1 Like

Before you encounter trouble, you will need to rename at least one file. We cannot import from the file “continue.py” because “continue” is a Python keyword and so it doesn’t think it’s a file and crashes. Changing the file name will fix that.

1 Like

ok so i replace it with resume? and if i use an app like pycharm is it better?

Anything not a Python keyword works:

choice = int(input("Which file to run?\n1) calculator\n2) continueOn\n"))+1
# Get user input
if choice == 1: # If user chooses option 1
  from calculator import main # Import the function "main" from
  # "otherFile.py" in the same folder
  main() # This executes the "main" function in "otherFile.py"
elif choice == 2:
  from continueOn import main # So "continue" is not valid because it is a Python keyword. Please rename it.
  main()
else:
  print("Your selection is invalid. Have a nice day.") # Change this to
  # whatever you want
2 Likes

also is it only limited for 2 scripts or more?

Just keep filling up with elif statements.

1 Like

ok thanks your truly a life saver

You may find useful to just make a list of names and use a function to get them. I have included it in this post or you can install it following these steps:
In shell, run pip install coderelijah
Then use the code here.

Or copy the function code (modified from something InvisibleOne made):
clear = lambda: print("\033c", end="", flush=True)
def getInput(prompt: str, options: list, inputLine: str = '=> ', error: str = 'Invalid Input', clearScreen: str = 'no'):
  """`prompt`: The question the user is asked.
  `options`: The options available to the user (must be in the form of a list)
  `inputLine`: The text displayed on the line that the user types in their input. If you want it to be blank, set it to '' or "".
  `error`: The message displayed when the user's input is invalid.
  Adapted from @InvisibleOne's post here:
  https://ask.replit.com/t/how-to-validate-user-input-properly/9586/2?
  Thanks also to @QwertyQwerty54 for the help with the docstrings.
  """
  while True: # This runs until it is ended
    print(prompt) # Display the prompt
    for option in options: # Goes through the whole list of options
      print(f"{options.index(option)+1} {option}") # Displays the option number and text
      
    userInput = input(inputLine) # Gets user input, dipslaying the text from "inputLine"
    
    try: # This stops the code from crashing when it gets errors
      userInput = int(userInput) # Convert user's input into a number
      
      if options[userInput-1]: # This returns the value of the number the user inputted as their answer. The text is not actually used and Python just uses the numbers
        return userInput-1
    except: # If anything went awry (such as the user putting in text)
      if clearScreen in ('yes', 'y', 'clear', 'clear()'): # User must set it to clear manually from the function options
        clear() # Clear screen
    else:
      print() # Displays blank line
      pass # Keep going
    
    print(error) # Display error message

It’s up to you whether you want to add the code yourself or import it. I’m not trying to self-advertise. If you do want to import it, please follow the steps above and also look here.

1 Like

also its giving me an error after every code execution, the code is working completely fine, but the error is still there is it supposed to happen?