2013-07-28 63 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()输出一个换行符,所以也许它唯一的原因是添加一个换行符?