首页 > 解决方案 > if 语句中的多个 if 语句都依赖于输入?

问题描述

我的目标是让每个输入激活每个打印当前代码:

import time

P = 95

west = 'west'

north = 'north'

south = 'south'

east = 'east'

lamp = 'lamp' 'light fixture' 'lamp'

grab = 'grab', 'get,', 'retrive','take'

go = 'go', 'tavel', 'walk', 'run'

put_down = 'place', 'drop', 'put'
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#*similarity functions*#
from difflib import SequenceMatcher

def similar(a, b):
  return SequenceMatcher(None, a, b).ratio()
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#print("wait for it")
#time.sleep(10)
#print("i just wasted 10 seconds of your life")
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
def lampIn(stat):
  return 'lamp' in stat or 'light fixture' in stat

def grabIn(stat):
  return 'grab' in stat or 'get,' in stat or 'retrive' in stat or 'take' in stat

def westIn(stat):
  return 'west' in stat

def northIn(stat):
  return 'north' in stat

def southIn(stat):
  return 'south' in stat

def eastIn(stat):
  return 'east' in stat

def grabIn(stat):
  return 'grab' in stat or 'get' in stat or 'retrieve' in stat or 'take' in stat

def goIn(stat):
  return 'go' in stat or 'travel' in stat or 'walk' in stat or 'run'

def put_downIn(stat):
  return 'place' in stat or 'drop' in stat or 'put' in stat
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
while True:
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  statement = input() 
  statement = statement.lower()
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  resultGrab = similar(grab, statement)
  grabResult = grabIn(statement)

  for grabResult in statement or resultGrab >= P:
    print("what are you getting?")
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++#
    statement = input() 
    statement = statement.lower()
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++#
    resultLamp = similar(lamp, statement)
    lampResult = lampIn(statement)

    for lampResult in statement or resultLamp >= P:
      print("you now have the lamp")
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  resultGo = similar(go, statement)
  goResult = goIn(statement)

  for goResult in statement or resultGo >= P:
    print("Where?")
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++#
    statement = input() 
    statement = statement.lower()
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++#
    resultWest = similar(west, statement)
    westResult = westIn(statement)
    for westResult in statement or resultWest >= P:
      print("You went west.")

else: print("not a command yet")

结果:

>>>walk
  what are you getting?
  est
  you now have the lamp
  you now have the lamp
  you now have the lamp
  you now have the lamp
  what are you getting?

我试图达到的结果:

>>>walk
   where?
   west
   you went west

同时还维护每个单独的路径

标签: pythonif-statementinput

解决方案


推荐阅读