2013-07-28 62 views
1

對於這可能是多麼簡單,我表示歉意,但是我看到這段代碼的一部分有點困惑。這個打印功能爲什麼在這裏?

# Geek Translator 
# Demonstrates using dictionaries 

geek = {"404": "clueless. From the web error message 404, meaning page not found.", 
     "Googling": "searching the Internet for background information on a person.", 
     "Keyboard Plague": "the collection of debris found in computer keyboards.", 
     "Link Rot" : "the process by which web page links become obsolete.", 
     "Percussive Maintainance" : "the act of striking an electronic device to make it work.", 
     "Uninstalled" : "being fired. Especially popular during the dot-bomb era."} 

choice = None 
while choice != "0": 

    print(
    """ 
    Geek Translator 

    0 - Quit 
    1 - Look Up a Geek Term 
    2 - Add a Geek Term 
    3 - Redefine a Geek Term 
    4 - Delete a Geek Term 
    """ 
    ) 

    choice = input("Choice: ") 
    print() 

    # exit 
    if choice == "0": 
     print("Good-bye.") 

    # get a definition  
    elif choice == "1": 
     term = input("What term do you want me to translate?: ") 
     if term in geek: 
      definition = geek[term] 
      print("\n", term, "means", definition) 
     else: 
      print("\nSorry, I don't know", term) 

    # add a term-definition pair   
    elif choice == "2": 
     term = input("What term do you want me to add?: ") 
     if term not in geek: 
      definition = input("\nWhat's the definition?: ") 
      geek[term] = definition 
      print("\n", term, "has been added.") 
     else: 
      print("\nThat term already exists! Try redefining it.") 

    # redefining an existing term 
    elif choice == "3": 
     term = input("What term do you want me to redefine?: ") 
     if term in geek: 
      definition = input("What's the new definition?: ") 
      geek[term] = definition 
      print("\n", term, "has been redefined.") 
     else: 
      print("\nThat term doesn't exist! Try adding it.") 

    # delete a term-definition pair 
    elif choice == "4": 
     input("What term do you want me to delete?") 
     if term in geek: 
      del geek[term] 
      print("\nOkay, I deleted", term) 
     else: 
      print("\nI can't do that!", term, "doesn't exist in the dictionary.") 

    # some unknown choice 
    else: 
     print("\nSorry, but", choice, "isn't a valid choice.") 

input("\n\nPress the enter key to exit.") 

我瞭解這一切之後choice = input(Choice: ")

與print()函數的異常工作,這是爲什麼呢?如果我刪除它,沒有什麼變化(據我所知),所以我很好奇它的意義。

+0

這是一個很好的問題。然而,我建議的一件事是,您使用註釋來標記讓您困惑的代碼行,並且在一般情況下,輸入/輸出是一個問題(這裏不是一個問題),您將包括示例輸入和輸出。更多信息在http://sscce.org和http://whathaveyoutried.com – Marcin

回答

2

print()不帶參數打印換行符。重點是在終端輸出中顯示一個空行。

+1

謝謝你的回覆=] – mccdlibby

+0

@mccdlibby不客氣! – Marcin

+1

和'print(end =「」)'將不會打印任何東西。 –

0

它會打印一個新行(在控制檯輸出中顯示爲空行)。

+0

謝謝,哈哈。起初我一無所知。 – mccdlibby

0

一個空的print()輸出一個換行符,所以也許它唯一的原因是添加一個換行符?