2018-10-15作業(yè)

import sys
import json
a = 'py1807'
all_student = {'user1': ['hjj', {'name': 'hjj', 'age': 12, 'phone': 1234, 'stu_id': 'py1807000'}]}
bian = '* '*30
f = '>>>>'
# lstrip()

def add_student(user):

        global f
        global bian
        pd = 1

        while pd == 1:
            with open('/Users/hujianjun/Desktop/student.json', 'r') as f:
                stu_message = json.load(f)
            num = stu_message[user][len(stu_message[user]) - 1]['stu_id']
            new_num = ''
            for x in num[6:10]:
                new_num += x
            new_num = str(new_num).lstrip('0')

            num = int(new_num) + 1
            stu_id = '%s%s' % (a, str(num).rjust(4, '0'))
            stu_name = input('請(qǐng)輸入學(xué)生姓名:'.rjust(19, ' '))
            stu_age = input('請(qǐng)輸入學(xué)生年齡:'.rjust(19, ' '))
            stu_phone = input('請(qǐng)輸入學(xué)生電話:'.rjust(19, ' '))
            stu_message[user].append(stu_name)
            stu_message[user].append({'name': stu_name, 'age': stu_age, 'phone': stu_phone, 'stu_id': '%s' % stu_id})

            with open('/Users/hujianjun/Desktop/student.json', 'w') as f1:
                json.dump(stu_message, f1)
            print('添加成功!?。?.rjust(19, ' '))
            b = '1.繼續(xù)添加'
            c = '2.返回上一頁(yè)'
            pd11 = input('%s\n%s\n%s\n%s' % (bian.rjust(71, ' '), b.rjust(41, ' '), c.rjust(42, ' '),
                                                 bian.rjust(71, ' ')))
            if pd11 == '2':
                break
            elif pd11 == '1':
                continue
            else:
                b = '你的輸入有誤返回到上一層'
                print('%s\n' % b.rjust(44, ' '))
                break
        

def select_student(user):
    global f
    global bian
    pd = 2
    while pd == 2:
        b = '1.查詢所有學(xué)生信息'
        c = '2.根據(jù)學(xué)生姓名查詢信息'
        pd_select = input('%s\n%s\n%s\n%s' % (bian.rjust(71, ' '), b.rjust(41, ' '), c.rjust(42, ' '), bian.rjust(71, ' ')))
        with open('/Users/hujianjun/Desktop/student.json', 'r') as f:
            stu_message = json.load(f)
        if pd_select == '1':
            for x in stu_message[user]:
                print(' '*10, x)
            b = '1.繼續(xù)查詢'
            c = '2.返回上一頁(yè)'
            pd11 = input('%s\n%s\n%s\n%s' % (bian.rjust(71, ' '), b.rjust(41, ' '), c.rjust(42, ' '), bian.rjust(71, ' ')))

            if pd11 == '2':
                break
            elif pd11 == '1':
                pass
            else:
                b = '你的輸入有誤請(qǐng)重新輸入'
                print('%s\n' % b.rjust(44, ' '))
                continue

        elif pd_select == '2':
            name_index = 0
            stu_pd = 0
            b = '請(qǐng)輸入你想要查詢的學(xué)生姓名:'
            name_select = input('%s' % b.rjust(42, ' '))
            for x in stu_message[user]:
                name_index += 1
                if name_select == x:
                    print(' '*10, stu_message[user][name_index])
                    stu_pd = 1
            if stu_pd == 0:
                b = '你查找的學(xué)生不存在'
                print('%s' % b.rjust(40, ' '))
            b = '1.繼續(xù)查詢'
            c = '2.返回上一頁(yè)'
            pd11 = input('%s\n%s\n%s\n%s' % (bian.rjust(71, ' '), b.rjust(41, ' '), c.rjust(42, ' '), bian.rjust(71, ' ')))

            if pd11 == '2':
                break
            elif pd11 == '1':
                pass
            else:
                b = '你的輸入有誤返回到上一層'
                print('%s\n' % b.rjust(44, ' '))
                break
        else:
            b = '你的輸入有誤重新輸入'
            print('%s\n' % b.rjust(44, ' '))
            continue


def delete_student(user):
    pd = 3
    global f
    global bian
    while pd == 3:
        pd_delete = input('請(qǐng)輸入你想要?jiǎng)h除的學(xué)生姓名:')

        index = 0
        name_index = []
        name_pd = 0
        with open('/Users/hujianjun/Desktop/student.json', 'r') as f:
            stu_message = json.load(f)
        if len(stu_message[user]) == 1:
            print('學(xué)生信息為空無(wú)法進(jìn)行刪除操作,返回到主頁(yè)面')
            break
        else:
            for x in stu_message[user][:]:
                index += 1
                if pd_delete == x:
                    name_index.append(index)
                    name_pd = 1
            if name_pd == 0:
                print('你輸入的學(xué)生不存在,請(qǐng)重新輸入')
                continue
            print(len(name_index))
            for x1 in range(len(name_index)):
                print('序號(hào):%d' % name_index[x1], stu_message[user][name_index[x1]])
            qr_delete = input('請(qǐng)確認(rèn)刪除信息,輸入序號(hào)進(jìn)行刪除,r.重新輸入,q.返回上一層\n')

            if qr_delete == 'r':
                continue
            elif qr_delete == 'q':
                break
            else:
                if int(qr_delete) not in name_index[:]:
                    print(qr_delete, name_index)
                    print('你輸入序號(hào)有誤重新輸入')
                else:

                    qr = int(qr_delete)

                    stu_message[user].pop(qr)
                    stu_message[user].pop(qr-1)
                    with open('/Users/hujianjun/Desktop/student.json', 'w') as f:
                        json.dump(stu_message, f)
                    # del stu_message[user][int(qr_delete)-1]
                    # del stu_message[user][int(qr_delete)]
                    print('刪除成功')
                    qr_1_delete = input('1.繼續(xù)刪除2返回上一層')
                    if qr_1_delete == '1':
                        continue
                    elif qr_1_delete == '2':
                        break
                    else:
                        print('輸入信息有誤返回到上一層!')




def manue(user):

    while  True:
        print(' '*10, '* * * * * * * * * * * * hjj學(xué)生管理系統(tǒng) * * * * * * * * * * *\n'
                      '           *                                                         *')
        b = '2.查找學(xué)生'
        c = '3.刪除學(xué)生'
        d = '4.退出系統(tǒng)'
        e = '* '*30

        g = '*                                                         *'
        a = '1.添加學(xué)生\n%s\n%s\n%s\n%s\n%s\n%s\n%s' % (g.rjust(70, ' '), b.rjust(41, ' '), g.rjust(70, ' '),\
                                                                c.rjust(41, ' '), g.rjust(70, ' '),d.rjust(41, ' '), e.rjust(71, ' '))
        pd1 = input(a.rjust(452, ' '))
        if pd1 == '1':
            add_student(user)

        elif pd1 == '2':
            select_student(user)
        elif pd1 == '3':
            delete_student(user)
        elif pd1 == '4':
            sys.exit()
        else:
            b = '你的輸入有誤請(qǐng)重新輸入'
            print('%s\n' % b.rjust(44, ' '))
            pd1 = '1'


# manue('user1')
def login():
    while True:
        print('1.登錄')
        print('2.注冊(cè)')
        login_pd = input()
        with open('/Users/hujianjun/Desktop/user.json', 'r') as f:
            user_massage = json.load(f)

        if login_pd == '2':

            while True:
                user = input('請(qǐng)輸入賬號(hào)')
                pwd = input('請(qǐng)輸入密碼')
                message = {user: pwd}
                pd = 0
                for x in user_massage:
                    if x == user:
                        pd = 1
                        print('該賬號(hào)已經(jīng)被注冊(cè)!請(qǐng)重新注冊(cè)')
                        continue
                if pd == 0:
                    user_massage[user] = pwd
                    print(user_massage)
                    with open('/Users/hujianjun/Desktop/user.json', 'w') as f1:
                        json.dump(user_massage, f1)

                    with open('/Users/hujianjun/Desktop/student.json', 'r') as f:
                        user_massage = json.load(f)
                    user_massage[user] = ['hjj', {'name': 'hjj', 'age': 12, 'phone': 1234, 'stu_id': 'py18070001'}]
                    with open('/Users/hujianjun/Desktop/student.json', 'w') as f2:
                        json.dump(user_massage, f2)
                    print('注冊(cè)成功!!返回到主頁(yè)')
                    break
        elif login_pd == '1':
            while True:
                user = input('請(qǐng)輸入賬號(hào)')
                pwd = input('請(qǐng)輸入密碼')
                pd1 = 0
                for x in user_massage:
                    if user == x:
                        pd1 = 1
                        if user_massage[x] == pwd:
                            pd1 = 2
                            break
                if pd1 == 2:
                    manue(user)
                elif pd1 == 1:
                    print('密碼錯(cuò)誤!請(qǐng)重新輸入')
                elif pd1 == 0:
                    print('賬戶不存在請(qǐng)查證后輸入')
login()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容