2013-10-04 58 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()