2013-10-04 6 views
-3

여기 내 미완성 전화 번호부가 있습니다. 사람들이 사전에서 사람들을 검색, 추가 및 제거 할 수 있기를 바랍니다. remove 함수를 아직 추가하지 않았지만 키워드를 통해 요청할 때이 스크립트를 다시 시작하는 방법은 무엇입니까? (예 : 다른 기능을 수행 하시겠습니까? [Y/N])이 사전을 반복하는 방법?

그렇다면 각 기능 (또는 명령문)을 처음부터 다시 시작하려면 어떻게해야합니까?

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

나는 당신이이 태그 할 때 당신이 당신의 자신의 질문에 대답 생각'사용자가 선택한 옵션이 "N"루프가없는 동안 – Harrison

+0

는, while 루프를 사용 loops'. –

답변

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() 
관련 문제