如何更改代码以便输入关键字?

时间:2016-02-16 22:09:39

标签: python python-3.x

我有一个代码,当它运行时 - 你会选择1到4.但是,我想改变它,所以我可以只输入关键字。例如,它可以说"您的代码有什么问题"我回答说"我的屏幕被冻结了#34;这将带我到屏幕冻结的问题。

非常感谢你的帮助,因为我对python很新,并且不太了解术语。如果您能告诉我将添加的代码放在哪里或在代码中回复它,我将不胜感激。

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()

2 个答案:

答案 0 :(得分:0)

您可以创建一个包含所有可能问题的列表。例如:

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!

答案 1 :(得分:0)

好吧也许我的问题是对的:

您可以使用正则表达式搜索关键字。但是因为你是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的另一个主题:http://www.nltk.org/ 如果有帮助,请接受答案。