2017-02-17 161 views
-3

我試圖運行這個Python程序,我發現它在這個網站,但是當我運行它沒有任何反應,我該如何運行此代碼?我是否需要向代碼添加任何內容?我正在使用這個網站來運行它repl.it:非常感謝。 ................我試圖運行這個程序,我發現在這個網站,但它不工作

#! /usr/bin/env python3 

# The following is a database of problems, keywords, and solutions. 
PROBLEMS = (('My phone does not turn on.', 
      {'power', 'turn', 'on', 'off'}, 
      ('Smack it with a hammer.', 
       'Wrap your phone in duck tape.', 
       'Throw it into the ocean.')), 
      ('My phone is freezing.', 
      {'freeze', 'freezing'}, 
      ('Dowse it in a petroleum-based product.', 
       'Light a match or find a suitable flame source.', 
       'Barbecue your phone until it is well done.')), 
      ('The screen is cracked.', 
      {'cracked', 'crack', 'broke', 'broken', 'screen'}, 
      ('Find some super glue.', 
       'Spread the super glue over the screen of the phone.', 
       'Either sit on the phone or place a 100 pounds over it.')), 
      ('I dropped my phone in water.', 
      {'water', 'drop', 'dropped'}, 
      ('Blow dry your phone with air below zero degrees Celsius.', 
       'Bake it in your oven at three hundred degrees Celsius.', 
       'Leave your phone on your roof for one week.'))) 


# These are possible answers accepted for yes/no style questions. 
POSITIVE = tuple(map(str.casefold, ('yes', 'true', '1'))) 
NEGATIVE = tuple(map(str.casefold, ('no', 'false', '0'))) 


def main(): 
    """Find out what problem is being experienced and provide a solution.""" 
    description = input('Please describe the problem with your phone: ') 
    words = {''.join(filter(str.isalpha, word)) 
      for word in description.lower().split()} 
    for problem, keywords, steps in PROBLEMS: 
     if words & keywords: 
      print('This may be what you are experiencing:') 
      print(problem) 
      if get_response('Does this match your problem? '): 
       print('Please follow these steps to fix your phone:') 
       for number, step in enumerate(steps, 1): 
        print('{}. {}'.format(number, step)) 
       print('After this, your phone should work.') 
       print('If it does not, please take it to a professional.') 
       break 
    else: 
     print('Sorry, but I cannot help you.') 


def get_response(query): 
    """Ask the user yes/no style questions and return the results.""" 
    while True: 
     answer = input(query).casefold() 
     if answer: 
      if any(option.startswith(answer) for option in POSITIVE): 
       return True 
      if any(option.startswith(answer) for option in NEGATIVE): 
       return False 
     print('Please provide a positive or negative answer.') 


if __name__ == '__main__': 
    main() 

回答

3

在該站點repl.it,最終if之前,我加入print(__name__),並得到輸出builtins。所以我們看到,在repl.it上運行時,程序失敗了很重要的條件,即__name__ == 'main'

您可以只用只此調用替換程序的那些最後兩行修改程序,使其在repl.it工作的功能main

main() 
0

如果您正在運行上該腳本網站你需要自己調用主要功能。在右欄請輸入:

main() 

你讓主函數執行。這被編譯成字節碼,並且在那個網站上你基本上啓動了解釋器並且導入了腳本,但是你仍然需要調用這個函數。

如果您在安裝有python解釋器的計算機上,則實際上可以鍵入python name_of_file.py,並且該腳本將通過執行最後一條if語句來運行該腳本。

0

當你做了什麼亞倫曼斯海姆說,你需要提供一些輸入。

點擊「輸入」在屏幕的右上角,然後在腳本中提到,如替代品的一個關鍵,

My phone does not turn on. 
yes 

然後點擊「設置輸入」。然後再次運行代碼。

順便說一句,這也許是我能想象學習如何使用Python代碼的最慢的方法之一。你應該在你的機器上安裝Python並以這種形式使用它。我的兩分錢。

相關問題