如何将关键字放入我的代码中?
How Can I Put Keywords Into My Code?
好吧,那么我该如何编写我的代码,而不是将 1 到 4,我可以只输入一个关键字,如 "Freezing" 或 "Froze" 我如何在我的代码中添加一个关键字搜索器,非常感谢所有帮助,在此先感谢您:)
def menu():
print("Welcome to Kierans Phone Troubleshooting program")
print("Please Enter your name")
name=input()
print("Thanks for using Kierans Phone Troubleshooting program "+name +"\n")
def start():
select = " "
print("Would you like to start this program? Please enter either y for yes or n for no")
select=input()
if select=="y":
troubleshooter()
elif select=="n":
quit
else:
print("Invalid please enter again")
def troubleshooter():
print("""Please choose the problem you are having with your phone (input 1-4):
1) My phone doesn't turn on
2) My phone is freezing
3) The screen is cracked
4) I dropped my phone in water\n""")
problemselect = int(input())
if problemselect ==1:
not_on()
elif problemselect ==2:
freezing()
elif problemselect ==3:
cracked()
elif problemselect ==4:
water()
start()
def not_on():
print("Have you plugged in the charger?")
answer = input()
if answer =="y":
print("Charge it with a diffrent charger in a diffrent phone socket. Does it work?")
else:
print("Plug it in and leave it for 20 mins, has it come on?")
answer = input()
if answer=="y":
print("Are there any more problems?")
else:
print("Restart the troubleshooter or take phone to a specialist\n")
answer=input()
if answer =="y":
print("Restart this program")
else:
print("Thank you for using my troubleshooting program!\n")
def freezing():
print("Charge it with a diffrent charger in a diffrent phone socket")
answer = input("Are there any more problems?")
if answer=="y":
print("Restart the troubleshooter or take phone to a specialist\n")
else:
print("Restart this program\n")
def cracked():
answer =input("Is your device responsive to touch?")
if answer=="y":
answer2 = input("Are there any more problems?")
else:
print("Take your phone to get the screen replaced")
if answer2=="y":
print("Restart the program or take phone to a specialist\n")
else:
print("Thank you for using my troubleshooting program!\n")
def water():
print("Do not charge it and take it to the nearest specialist\n")
menu()
while True:
start()
troubleshooter()
您正在寻找 enum
from enum import Enum
class Status(Enum):
NOT_ON = 1
FREEZING = 2
CRACKED = 3
WATER = 4
然后在你的 if
支票中,你做这样的事情:
if problemselect == Status.NOT_ON:
...
elif problemselect == Status.FREEZING:
...
由于您希望用户输入 phone 问题的描述,因此您可能需要一个问题列表以及与这些问题相关的关键字。下面的程序展示了如何安排这样一个数据库,以及如何根据用户的输入来搜索它。这个解决方案有更快的替代方案(例如索引数据库),但这个基本实现暂时应该足够了。您将需要提供有关如何解决问题的更多代码和信息,但您对上一个问题的回答应该有助于指导您解决这方面的问题。
#! /usr/bin/env python3
# The following is a database of problem and keywords for those problems.
# Its format should be extended to take into account possible solutions.
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'}))
# 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 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 ...')
# Provide code that shows how to fix the problem.
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 到 4,我可以只输入一个关键字,如 "Freezing" 或 "Froze" 我如何在我的代码中添加一个关键字搜索器,非常感谢所有帮助,在此先感谢您:)
def menu():
print("Welcome to Kierans Phone Troubleshooting program")
print("Please Enter your name")
name=input()
print("Thanks for using Kierans Phone Troubleshooting program "+name +"\n")
def start():
select = " "
print("Would you like to start this program? Please enter either y for yes or n for no")
select=input()
if select=="y":
troubleshooter()
elif select=="n":
quit
else:
print("Invalid please enter again")
def troubleshooter():
print("""Please choose the problem you are having with your phone (input 1-4):
1) My phone doesn't turn on
2) My phone is freezing
3) The screen is cracked
4) I dropped my phone in water\n""")
problemselect = int(input())
if problemselect ==1:
not_on()
elif problemselect ==2:
freezing()
elif problemselect ==3:
cracked()
elif problemselect ==4:
water()
start()
def not_on():
print("Have you plugged in the charger?")
answer = input()
if answer =="y":
print("Charge it with a diffrent charger in a diffrent phone socket. Does it work?")
else:
print("Plug it in and leave it for 20 mins, has it come on?")
answer = input()
if answer=="y":
print("Are there any more problems?")
else:
print("Restart the troubleshooter or take phone to a specialist\n")
answer=input()
if answer =="y":
print("Restart this program")
else:
print("Thank you for using my troubleshooting program!\n")
def freezing():
print("Charge it with a diffrent charger in a diffrent phone socket")
answer = input("Are there any more problems?")
if answer=="y":
print("Restart the troubleshooter or take phone to a specialist\n")
else:
print("Restart this program\n")
def cracked():
answer =input("Is your device responsive to touch?")
if answer=="y":
answer2 = input("Are there any more problems?")
else:
print("Take your phone to get the screen replaced")
if answer2=="y":
print("Restart the program or take phone to a specialist\n")
else:
print("Thank you for using my troubleshooting program!\n")
def water():
print("Do not charge it and take it to the nearest specialist\n")
menu()
while True:
start()
troubleshooter()
您正在寻找 enum
from enum import Enum
class Status(Enum):
NOT_ON = 1
FREEZING = 2
CRACKED = 3
WATER = 4
然后在你的 if
支票中,你做这样的事情:
if problemselect == Status.NOT_ON:
...
elif problemselect == Status.FREEZING:
...
由于您希望用户输入 phone 问题的描述,因此您可能需要一个问题列表以及与这些问题相关的关键字。下面的程序展示了如何安排这样一个数据库,以及如何根据用户的输入来搜索它。这个解决方案有更快的替代方案(例如索引数据库),但这个基本实现暂时应该足够了。您将需要提供有关如何解决问题的更多代码和信息,但您对上一个问题的回答应该有助于指导您解决这方面的问题。
#! /usr/bin/env python3
# The following is a database of problem and keywords for those problems.
# Its format should be extended to take into account possible solutions.
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'}))
# 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 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 ...')
# Provide code that shows how to fix the problem.
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()