在这段代码中我可以实现一个while循环吗?

2024-10-01 02:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个非常新的编码,所以这个问题可能是非常愚蠢和明显的,但我应该把我的循环,使斯图尔不会重置每次我添加一个新的键到字典,因为到目前为止,每次我都试图输入循环的键,已添加的用户不断重置

import time
StuL = {"1317281" : "Name : Reese John ID :1317281 DoB : 12/11/95 Address 
: 57 Fake Road Phone Number : 02087475632 Gender : Male Tutor Group : 10K  
Email : JohnReese@HighSchool.com"} 
UserName = input ("Enter Username: ")
PassWord = input ("Enter Password: ")
if UserName == "MrLeeman" and PassWord == "hunter2":
    time.sleep(1)
    print ("Login successful")
    time.sleep(1)
    ch=(input("Enter your choice\n1:View student details\n2:Add 
    Students\n3:Exit"))
    if ch == "1":
        time.sleep(1)
        inp = input("Enter student's ID number")
        if inp in StuL:
            time.sleep(1)
            print(StuL[inp])
    elif ch == "2":
        time.sleep(1)
        edit=input("Enter the ID for the student you wish to add")
        inf=input("Enter the information for the student in the following 
        order\nName,ID,DoB,Address,Phone number,Gender,Tutor 
        Group,Email")
        StuL[edit] = inf
    elif ch == "3":
        break


   else:
        print("Invalid option")
else:
        print ("Password did not match")

Tags: theidinputiftimeaddresssleepch
1条回答
网友
1楼 · 发布于 2024-10-01 02:24:40

我希望你能从中学到一些东西:

#!python3
#coding=utf-8

import sys
print(sys.version)

StuL = {
    "1": {
        "Name": "Reese John",
        "ID": "1317281",
        "DoB": "12/11/95",
        "Address": "57 Fake Road",
        "Phone Number": "02087475632",
        "Gender": "Male",
        "Tutor Group": "10K",
        "Email": " JohnReese@HighSchool.com"
        }
}

UserName = input("Enter Username: ")
PassWord = input("Enter Password: ")
if UserName == "u" and PassWord == "p":
    print("Login successful")

    while(True):
        ch = input("Enter your choice\n1:View student details\n2:Add Students\n3:Exit\n")
        if ch == "1":
            inp = input("Enter student's ID number")
            if inp in StuL:
                print(StuL[inp])

        elif ch == "2":
            edit = input("Enter the ID for the student you wish to add")
            inf = input( "Enter the information for the student in the following order\nName,ID,DoB,Address,Phone number,Gender,Tutor Group,Email\n")
            StuL[edit] = inf
            # this does not create a Dictionary! Input needs to be parsed or separate.
        elif ch == "3":
            break
        else:
            print("Invalid option")
else:
    print ("Password did not match")

相关问题 更多 >