2013-10-04 91 views
-3

在這裏我有我未完成的電話簿。我希望人們能夠搜索,添加和刪除字典中的人。我還沒有添加刪除功能,但是如何在通過關鍵字請求時允許此腳本重新開始。 (EG。你想執行另一個功能嗎?[是/否])如何循環這本詞典?

那麼,我將如何使每個函數(或者語句)返回到開始?

details = {"Toby": "123", "James": "234", "Paul": "345"} 


print("Welcome To The Telephone Directory.\n") 


print("To search out specific details, please type Search.\n To add a new person to the Directory, type Add.\n To change someone's details, please type Edit.") 

inputKey = input(); 


if(input() == inputKey.strip() in "search Search SEARCH".split()): 



     print("Please enter the name of the customer.") 
     customer = input() 
     print("Number:" ,details.get(customer)) 
     if(customer not in details): 
      print("I'm sorry, but the person you defined is not in the directory.") 


elif(input() == inputKey.strip() in "add Add ADD AdD aDD".split()): 


    print("Please enter the full name of the person you would like to add to the directory.") 
    addedcustomer = input() 
    print("Now, please enter their number.") 
    addednumber = int(input()) 
    details.add [addedcustomer : addednumber] 
    print(details) 
+2

我想你回答了你自己的問題,當你標記這與'loops' – Harrison

+0

使用while循環,而由用戶選擇的選項不是「N」循環。 –

回答

3

while: loop在你的現有代碼:

while True: 
    print("Welcome To The Telephone Directory.\n") 
    print("To search out specific ...") 

    # All of your existing stuff goes here 
    ... 

    # This goes at the very end of your loop: 
    yorn = input("Do you want to look up another one? ") 
    if yorn.lower() != "y": 
     break 
0

首先,操作字符串時,它通常是一個好主意,他們正常化以某種方式,例如所有較低或全部大寫:

def normalise(s): 
    return s.strip().lower() 

if normalise(input()) == normalise('ADD'): 
    ... 

要在你的腳本循環,可以簡單地...在你的腳本循環:

def process(cmd): 
    "Processes a command using very advanced text-analysis techniques" 
    return "You typed: %s" % cmd 

dorun = True 
print("Type your command here:") 
while dorun: 
    print(">") 
    cmd = input() 
    if normalise(cmd) == normalise('EXIT'): 
     dorun = False 
    else: 
     print(process(cmd)) 
print("Goodbye") 

一個典型的會議將是:

Type your command here: 
> hello 
You typed: hello 
> x 
You typed: x 
> exit 
Goodbye 

除了翻轉dorun標誌,您還可以直接退出解釋器,從您所在的功能(如果您是)或break退出循環。 無論如何,你想循環,直到你收到一些可識別的信號(來自用戶,環境,你的程序的狀態......),它告訴你退出。

0

這是您的代碼與我的修復程序。下次輸入代碼時請不要使用完整的腳本。隔離什麼是不工作和峯會。

# Dictionary of names/random number 
details = {"Toby": "123", "James": "234", "Paul": "345"} 

def welcome(): 
    print("Welcome To The Telephone Directory.\n") 

def tip(): 
    print("To search out specific details, please type Search.\n To add a new person to the Directory, type Add.\n To change someone's details, please type Edit.") 

def getinput(): 
    #input only works for numbers: raw input is what you want for strings 
    return raw_input("Command: "); 

def main(): 
    welcome() # print the welcome message 
    tip()  # print a tip to help user 

    while 1: # enter an infinte loop 
     usrinput = getinput() # get a input from the user 
     usrinput = usrinput.lower() # make string into a lowercase string 

     if usrinput == 'search': 

      print("Please enter the name of the customer.") 

      customer = raw_input("Name: ") # we need a string, use raw_input 

      print("Number:" , details.get(customer)) 
      if(customer not in details): 
       print("I'm sorry, but the person you defined is not in the directory.") 

     elif usrinput == 'add': 

      print("Please enter the full name of the person you would like to add to the directory.") 
      addedcustomer = raw_input("Enter a name to add: ") 
      print("Now, please enter their number.") 
      addednumber = input('Number: ') # no need to convert to int. input only works for numbers 
      details.add [addedcustomer : addednumber] 
      print(details) 

     elif usrinput == 'exit': 
      break 




if __name__ == '__main__': 
    main()