How to access separate python file in another python replit to store data like a data frame

Should be:

from Program_Files.hello_world import hello_world
hello_world()
1 Like

That Works too, if you just want the hello_world() function. But if you have more in that file, you should use my way, or lightdefusion’s way

No, if you have more functions you could just do:

from Program_Files.hello_world import x, y, z
x()
y()
z()
1 Like

Not to argue, but if you have, like 30 functions in the file, and you want all of them, my way would be more efficient.

@QwertyQwerty88

Or for efficiency:

from Program_Files.hello_world import *

How? :slight_smile:

Or for efficiency and bad practice*

You usually should not use import * as it’s bad practice.

3 Likes

Are we saying we can import filed from another repl there? That would be interesting,

1 Like

Lets say I have functions named a-z if I did this:

from Program_Files.hello_world import a, b, c, d, e, f, g, h, i, j,

and so on, while you could do this:

import Program_Files.hello_world

Then you do not need to enter all the commands.

it’s not more efficient though, as you still need to type Program_Files.hello_world.a() every time you wanted to call a function

anyway I think we’re getting too off-topic

LOL yeah! I guess you are right

I think this might be a solution to my issue. But my last question would be how did you create the program files file directory ?just by typing it in when you create a folder? And does the overall operating system recognize program files is where all your Replit’s programs reside?

or is it just sufficient to create a program files directory and put my post collection answer data from the students in another program ( inside program filescdirectory)

and another question would be if I have a student take the quiz on their phone or PC will I be able to access the data that it produces on my device?

The reason I ask this is I had a student run the quiz and the data collected on the answer text file I put only showed up on their phone.

how did you create the program files file directory ?just by typing it in when you create a folder?

Yes, just create a normal directory.

And does the overall operating system recognize program files is where all your Replit’s programs reside?

No, this is just a normal folder having no special meaning to the OS.

It would be better if you use a database for this. It depends whether the repl stops in between you accessing it (in that case all staged files are deleted iirc)

This means that the data wasnt saved (program stopped so the staged files deleted). So use a database to store responses

well the database i want to use is a dataframe. Are u suggesting SQL? if so how do I store data into these so I can grade each student. but to your point on program stopping . The program saves answers in text file before ending . so why is that deleted on stoping program.

def read_questions(filename):
    questions = []
    with open(filename, 'r') as file:
        for line in file:
            question = line.strip()
            questions.append(question)
    return questions

def read_answers(filename):
    answers = []
    with open(filename, 'r') as file:
        for line in file:
            answer = line.strip()
            answers.append(answer)
    return answers

def generate_output_file(student_name, grade, incorrect_answers_count, incorrect_answers):
    output_filename = student_name + "_grade.txt"
    with open(output_filename, 'w') as file:
        file.write("Student Name: {}\n".format(student_name))
        file.write("Grade: {}\n".format(grade))
        file.write("Number of Incorrect Answers: {}\n".format(incorrect_answers_count))
        file.write("Incorrect Answer(s): {}\n".format(', '.join(map(str, incorrect_answers))))
    print("Grade saved in {}.".format(output_filename))

def calculate_grade(answers, student_answers):
    total_questions = len(answers)
    correct_answers = 0
    incorrect_answers = []
    for i in range(total_questions):
        if answers[i].lower() == student_answers[i].lower():
            correct_answers += 1
        else:
            incorrect_answers.append(i + 1)
    grade = (correct_answers / total_questions) * 100
    return grade, len(incorrect_answers), incorrect_answers

def main():
    questions_file = "Questions.txt"
    answers_file = "Answers.txt"

    questions = read_questions(questions_file)
    answers = read_answers(answers_file)

    print("Welcome to the Medical Terminology Quiz!")
    print("Please enter your name to begin the quiz.")
    student_name = input("Name: ")

    student_answers = []
    for question in questions:
        print(question)
        answer = input("True or False? ")
        student_answers.append(answer)

    grade, incorrect_answers_count, incorrect_answers = calculate_grade(answers, student_answers)

    generate_output_file(student_name, grade, incorrect_answers_count, incorrect_answers)

if __name__ == '__main__':
    main()


File changes simply dont persist after stopping the program.

From https://docs.replit.com/power-ups/always-on

We recommend using Replit Database to persist information outside of your process.

3 Likes

I discovered the Replit database so I will see if this works on different devices. Thanks so much.

1 Like

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