Python 如何更改代码以便您可以键入关键字?
我有一个代码,当它运行时,会让你选择1到4。但是,我想更改它,这样我就可以只输入关键字。例如,它可能会说“你的代码出了什么问题”,而我的回答是“我的屏幕被冻结了”,这会把我带到屏幕冻结的问题 非常感谢您提供的帮助,因为我对python非常陌生,对术语也不太了解。如果您能告诉我将添加的代码放在哪里或在代码中用它回复,我将不胜感激Python 如何更改代码以便您可以键入关键字?,python,python-3.x,Python,Python 3.x,我有一个代码,当它运行时,会让你选择1到4。但是,我想更改它,这样我就可以只输入关键字。例如,它可能会说“你的代码出了什么问题”,而我的回答是“我的屏幕被冻结了”,这会把我带到屏幕冻结的问题 非常感谢您提供的帮助,因为我对python非常陌生,对术语也不太了解。如果您能告诉我将添加的代码放在哪里或在代码中用它回复,我将不胜感激 def menu(): print("Welcome to Kierans Phone Troubleshooting program") print("Plea
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()
您可以创建一个包含所有可能问题的列表。例如:
def troubleshooter():
problems = ["My phone doesn't turn on",
"My phone is freezing",
"The screen is cracked",
"I dropped my phone in water"]
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 = input()
if problemselect == problems[0]:
not_on()
elif problemselect == problems[1]:
freezing()
elif problemselect == problems[2]:
cracked()
elif problemselect == problems[3]:
water()
start()
如果您的意思是想找到某个单词,可以通过以下方法实现:
>>> sentence = "Hello World!"
>>> word = "Hello"
>>> if word in sentence:
print("Yep!")
Yep!
好吧,也许我答对了你的问题: 您可以使用正则表达式来搜索关键字。但是,由于您是python新手,最好使用
find()
,如下所示,我想这对于您的目的来说足够简单和准确,但并不完全健壮:
尝试使用以下函数定义替换troubleshooter()
:
def troubleshooter():
q = raw_input('Enter you problem : ')
q = q.lower()
freeze = ['freeze', 'froze', 'freezing', 'hang', 'hung'] #you can keep adding
boot = ['turn on', 'boot', 'off'] #you can again keep adding
screen = ['cracked', 'crack', 'broke', 'shattered', 'shatter'] #keep adding
water = ['water', 'liquid']
freeze_q = sum([q.find(keyword) for keyword in freeze])
boot_q = sum([q.find(keyword) for keyword in boot])
screen_q = sum([q.find(keyword) for keyword in screen])
water_q = sum([q.find(keyword) for keyword in water])
if freeze_q > -len(freeze):
# print 'freeze problem'
not_on()
elif boot_q > -len(boot):
# print 'boot problem'
freezing()
elif screen_q > -len(screen):
# print 'screen problem'
cracked()
elif water_q > -len(water):
# print 'water problem'
water()
else:
print 'invalid question'
使其健壮是NLP下的另一个主题:
如果答案有用,就接受它