如何回到Python中的某个点

时间:2018-03-23 23:39:13

标签: python python-3.x sqlite

我想在每次操作完成后返回主菜单,例如添加学生,删除等等....我只是不知道该怎么做,请帮忙。 还有一件事,如果我为每个操作创建函数它将如何工作?

PS:我很抱歉,如果有人已发布此问题,我无法找到与此问题相关的任何问题,而且我是新的,所以请放轻松! 的由于

import time
import sqlite3
import sys

""" Program Info """
AppName = "Student Manager"
AppVersion = "1.4.2V"
AppAuthor = "Zaidan Chaudhary"

print("{} || {} || {}".format(AppName, AppVersion, AppAuthor))
time.sleep(2)

""" Start Of Program """

print("\n***Welcome To Student Manager***")
print("Main Menu\n\n1: Login\n2: Sign Up\n3: Exit")


"""Start of Program """
def Start():


    """ Creating SQl Database and Table in it """
    con = sqlite3.connect("StudentManager.db")
    cursor = con.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS LoginSystem(firstName TEXT, lastName TEXT, username TEXT, password TEXT)")

    """Main Program """

    while True:
        try:
            InputMenu = int(input("Please select an option from Menu: "))
            break
        except ValueError:
            print("\nPlease enter in numbers [1, 2, 3]\n")

    if InputMenu == 1:
        LogUsername = input("Please enter your username: ")
        LogPassword = input("Please enter your password: ")
        cursor.execute("SELECT * FROM LoginSystem WHERE username = ? AND password = ?", (LogUsername, LogPassword))
        CheckingLogin = cursor.fetchall()


        if CheckingLogin:
            for i in CheckingLogin:
                print("Welcome " + i[0] + "" + i[1] + " to your dashboard\n")

        cursor.execute("CREATE TABLE IF NOT EXISTS Students(firstName TEXT, lastName TEXT, grade TEXT, "
                       "age TEXT, phoneNo TEXT, address TEXT)")
    ### This Is My MAIN MENU #####
        print("1: Add Students\n2: View Students\n3: Delete Students\n4: Exit")
        while True:
            try:
                UserEntry = int(input("Please select an option from Main Menu: "))
                break
            except ValueError:
                print("\nError!\nPlease enter in numbers...\n")
        if UserEntry == 1:
            StudentFirstName = input("Please enter student's first name: ")
            StudentLastName = input("Please enter student's last name: ")
            StudentGrade = input("Please enter student's grade: ")
            StudentAge = input("Please enter student's age: ")
            StudentPhoneNO = input("Please enter his/her parent's phone number: ")
            StudentAddress = input("Please enter student's home address: ")
            StudentFirstName.title()
            StudentLastName.title()
            StudentGrade.title()
            StudentPhoneNO.title()
            StudentAge.title()
            StudentAddress.title()
            cursor.execute("INSERT INTO Students VALUES(?, ?, ?, ?, ?, ?)",
                           (StudentFirstName, StudentLastName, StudentGrade, StudentAge, StudentPhoneNO,
                            StudentAddress))
            con.commit()
            print("Student Added Successfully!")
        elif UserEntry == 2:
            print("1: View All Students\n2: View By FirstName\n3: View By Grade")
            while True:
                try:
                    Entry = int(input("Please select an option from Menu: "))
                    break
                except ValueError:
                    print("Please select in numbers...\n")

            if Entry == 1:
                cursor.execute("SELECT * FROM Students")
                AllStudents = cursor.fetchall()
                AllStudents.sort()
                for b in AllStudents:
                    print("Student Name: {} ".format(b[0]) + "{}".format(b[1]))
                    print("Student grade: {}".format(b[2]))
                    print("Student age: {}".format(b[3]))
                    print("Phone Number: {}".format(b[4]))
                    print("Home Address: {}\n".format(b[5]))

            elif Entry == 2:
                ViewFirst = input("Please enter students first name: ")
                cursor.execute("SELECT * FROM Students WHERE firstName = ?", (ViewFirst,))
                StudentInfo = cursor.fetchall()
                for g in StudentInfo:
                    print("Student Name: {} ".format(g[0]) + "{}".format(g[1]))
                    print("Student grade: {}".format(g[2]))
                    print("Student age: {}".format(g[3]))
                    print("Phone Number: {}".format(g[4]))
                    print("Home Address: {}\n".format(g[5]))

            elif Entry == 3:
                Grade = input("Please enter student's grade/class: ")
                cursor.execute("SELECT * FROM Students WHERE grade = ?", (Grade,))
                StudentGradeInfo = cursor.fetchall()
                for p in StudentGradeInfo:
                    print("Student Name: {} ".format(p[0]) + "{}".format(p[1]))
                    print("Student grade: {}".format(p[2]))
                    print("Student age: {}".format(p[3]))
                    print("Phone Number: {}".format(p[4]))
                    print("Home Address: {}\n".format(p[5]))



        elif UserEntry == 3:
            DelName = input("Enter student's firstname you want to delete: ")
            DelName.title()
            cursor.execute("DELETE FROM Students WHERE firstName = ?", (DelName,))
            con.commit()
            AllStudents = cursor.fetchall()
            print("Student Deleted Successfully")
            for b in AllStudents:
                print(b)

        elif UserEntry == 4:
            print("Closing App......")
            time.sleep(2)
            sys.exit()





        else:
            print("Invalid Username or Password")


    elif InputMenu == 2:
        while True:
            firstName = input("Please enter your First Name: ")
            lastName = input("Please enter your Last Name: ")
            Username = input("Please enter new username: ")
            Password = input("Please enter password: ")
            ConfirmPass = input("Please re-enter password: ")

            if Password == ConfirmPass:
                cursor.execute("INSERT INTO LoginSystem VALUES(?, ?, ?, ?)", (firstName, lastName, Username, Password))
                con.commit()
                con.close()
                print("User Registered")

                break

            else:
                print("Error!\n Unable to Register\n Please recheck information")

    elif InputMenu == 3:
        print("Closing App....")
        time.sleep(2)
        sys.exit()

""" End Of Start Function """


Start()

1 个答案:

答案 0 :(得分:0)

我在伪代码中回答这个问题,它会更容易理解。

while True:

    if Operation happening:
        MainMenu.hide()
    elif Operation not happening:
        MainMenu.show()

无论函数是否重定向到不同的页面,这都应该有效,因为一旦函数完成,MainMenu就会显示(只有当它完全确定操作没有发生时,才会显示" elif&#34 )。

要回答第二个问题,请保持while循环运行。

running = True

while running:
    command = input('> ')
    if "this_happens":
        execute_function(params)

我知道这是最简单的方法。每当用户想要做某事时,他们都可以在这里输入。然后使用if语句检查用户想要做什么,然后执行它。你可以采用不同的方式,但这只是一个例子。