我尝试运行这个我在这个网站上找到它的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()
答案 0 :(得分:3)
在该网站repl.it上,在最终if
之前,我添加了print(__name__)
并获得了输出builtins
。所以我们看到,当在repl.it上运行时,程序失败了__name__ == 'main'
的非常重要的条件。
只需调用函数main
,就可以修改程序使其在repl.it上工作只需更换程序的最后两行:
main()
答案 1 :(得分:0)
如果您在该网站上运行该脚本,则需要自己调用main函数。在右栏中输入:
main()
使主函数执行。这被编译成字节代码,在那个网站上你基本上启动了解释器并导入了脚本,但你仍然需要调用该函数。
如果您所在的计算机上安装了python解释器,您实际上可以键入python name_of_file.py
,然后通过在那里执行最后一个if语句来运行该脚本。
答案 2 :(得分:0)
在你做了Aaron Mansheim所说的话之后,你需要提供一些意见。
点击'输入'在屏幕的右上角,然后键入脚本中提到的替代方案之一,如
My phone does not turn on.
yes
然后点击'设置输入'。然后再次运行代码。
顺便说一句,这可能是我学习如何使用Python代码的最慢的方法之一。您应该在您的计算机上安装Python并以该形式使用它。我的两分钱。