2017-05-07 26 views
-4

我對編碼非常陌生,所以這個問題可能非常愚蠢和明顯,但我應該在哪裏放置我的循環,以便StuL在每次向字典添加新密鑰時都不會重置,因爲每當我嘗試過時輸入迴路已經由用戶添加的按鍵保持復位我在哪裏可以在此代碼中實現一個while循環?

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 : [email protected]"} 
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") 
+1

請不要向我們提供代碼映像,以複製您的代碼的最喜歡的工具,並粘貼在這裏。 – Maroun

+2

歡迎來到Stack Overflow! [請不要將您的代碼作爲圖像發佈。](// meta.stackoverflow.com/q/285551) –

+0

除了發佈代碼,不包括代碼圖片,我發現很難猜測您正在嘗試做什麼 - 請更明確。如果你想修改一個循環,但不重置'StuL',確保你沒有在循環體中包含'StuL = {...}。 – tiwo

回答

0

我希望你從中學到的東西:

#!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": " [email protected]" 
     } 
} 

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") 
相關問題