-2

I was just wondering whether anyone could help me with this issue. I am currently designing a scoring program for a college tournament as part of my school project.

Right now, I have a main menu in a single .py file, but I have my register student function in a different .py file. I have so far managed to call the function into the main menu program & works the way I expected it to. However the one thing I experienced was that the function would simply loop itself non-stop and does not stop.

Because of this I called back the main menu function at the end of the register student function. This works as far as allowing you to register a student (whos name is then appended into a text file) and then sends you back to the main menu for the user to choose another action.

If I then try and enter 1 in again, it spits out a long traceback list and finally an error:

ModuleNotFoundError: No module named 'Function_1_reg_student.py';
    'Function_1_reg_student' is not a package.

How do I get my program to allow me to register a student, possibly ask the user whether they want to register another student or go back to the main menu if the user says no?

I apologise if this makes no sense, I am extremely new to this and find it quite hard to explain my situation and what I would like it to do!

Below is both of my files:

Main_menu.py:

def main_menu():
    print(""" Welcome to the main menu.              
    |  (1.)     Register a student|                      
    |  (2.)     Score a student   |                                         
    |  (3.)     Leaderboard       |                                             
    |  (4.)     Quit              |
    """)

    while True:
        answer=input("Please enter a number between 1-4: ")
        if answer=="1":
            from Function_1_reg_student.py import reg_solo
            continue
        elif answer=="2":
            print ("Call function 2")
            continue
        elif answer=="3":
            print ("Call function 3")
            continue
        elif answer=="4":
            print ("Quit")
            continue
        else:
            print("ERROR MESSAGE: Please enter in a number from 1 to 4")
            continue

main_menu()

Function_1_reg_student.py:

def reg_solo():
   while True:
      studentname=input("Please enter student name ")
      studentsurname=input("Please enter surname ")
      print("Name: "+studentname+" "+studentsurname)
      text_file=open("solo_students_reg.txt","a")


text_file.write(studentname+":"+studentsurname+":"+"0"+":"+studentname[:3]+    studentsurname[:3]+":"+"\n")
      text_file.close()
      print(studentname+" "+studentsurname+" has been registered")
      from MAIN_MENU.py import main_menu
reg_solo()
Prune
  • 76,765
  • 14
  • 60
  • 81

1 Answers1

2

Don't include the file extension when importing a module, or from a module.

Do this:

from Function_1_reg_student import reg_solo

Not this:

from Function_1_reg_student.py import reg_solo
Cyphase
  • 11,502
  • 2
  • 31
  • 32