簡體   English   中英

如何更改代碼,以便您輸入關鍵字?

[英]How do I change my code so you can type in keywords?

我有一個運行該代碼的代碼-請選擇1到4。但是,我想更改它,所以我可以直接輸入關鍵字。 例如,它可能會說“您的代碼有什么問題”,然后我回答“我的屏幕被凍結”,這將帶我進入屏幕凍結的問題。

非常感謝您的幫助,因為我對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()

您可以創建一個包含所有可能問題的列表。 例如:

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的另一個主題: http: //www.nltk.org/如果有幫助, 接受答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM