我如何拆分我的代码的解决方案?

时间:2016-02-19 23:30:17

标签: python python-3.x

当你给我的代码一个问题它告诉你你的问题,但我不能改变它所以它有多个解决方案它总是给所有不同的问题提供相同的解决方案?我要问的是如何为正确的问题提供正确的解决方案,而不是所有的问题只会导致一个解决方案,它说打印('你的问题的解决方案是...')我会喜欢改变它所以它为不同的问题提供了不同的解决方案:)提前谢谢你:))

 PROBLEMS = (('My phone does not turn on.',
             {'power', 'turn', 'on', 'off'}),
            ('My phone is freezing.',
             {'freeze', 'freezing'}),
            ('The screen is cracked.',
             {'cracked', 'crack', 'broke', 'broken', 'screen'}),
            ('I dropped my phone in water.',
             {'water', 'drop', 'dropped'}))


POSITIVE = tuple(map(str.casefold, ('yes', 'true','yeah', 'positive','thats right', 'yeah bro','yes', '1')))
NEGATIVE = tuple(map(str.casefold, ('no', 'false','na', 'not true','thats wrong', 'na bro','nope' '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 in PROBLEMS:
        if words & keywords:
            print('This may be what you are experiencing:')
            print(problem)
            if get_response('Does this match your problem? '):
                print('The solution to your problem is ...')
                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()

1 个答案:

答案 0 :(得分:1)

根据您的问题,下面的程序已经过修改,还包括可用于解决手机问题的步骤。关注关键字,现在包含在PROBLEMS数据库中的步骤列表。请用自己的步骤代替给出的步骤。提供的步骤是样本想法,但在试图帮助某人方面也是垃圾。如果您愿意,可以为每个解决方案添加更少或更多的步骤。

#! /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()