错误的条件匹配和输出

时间:2017-12-02 11:14:02

标签: python python-2.7 loops

import os
import sys
import subprocess
from subprocess import call
import sys, termios
print "A list of food that we recommend while you are in Rajasthan \n"
print "> Dal Bati Churma"
print "> Laal Maas"
print "> Gatte ki Khichdi"
print "> Rajasthani Kadi"
print "> Gatte ka Pulao"
print "> Churma Ladoo"
print "> Badam ka Halwa"
print "> Gujia"
print "I can show you a few food images"
show=raw_input("What do you want to see? \n")
while 1:
    if show is 'Gujia' or 'gujia': 
        call(['bash','gujia.sh'])
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)
        break
#   subprocess.call(["gujia.sh"])
    elif input is 'Dal Bati Churma' or 'dal bati churma':
        call(['bash','dbc.sh'])
        break
#   subprocess.call(['dbc.sh'])
    elif input is 'Laal Maas' or 'laal maas' or 'lal maas':
        call(['bash','lm.sh'])  
        break   
#   subprocess.call(['lm.sh'])
    elif input is 'Gatte ki Khichdi' or 'gatte ki khichdi':
        call(['bash','gkk.sh'])
        break
#   subprocess.call(['gkk.sh'])
    elif input is 'Rajasthani Kadi' or 'rajasthani kadi':
        call(['bash','rk.sh'])
        break
#   subprocess.call(['rk.sh'])
    elif input is 'Gatte ka Pulao' or 'gatte ka pulao':
        call(['bash','gkp.sh'])
        break
#   subprocess.call(['gkp.sh'])
    elif input is 'Churma Ladoo' or 'churma ladoo':
        call(['bash','cl.sh'])
        break
#   subprocess.call(['cl.sh'])
    elif input is 'Badam ka Halwa' or 'badam ka halwa':
        call(['bash','bkh.sh'])
        break
#   subprocess.call(['bkh.sh'])
    else: 
        print "I am not sure what you want?"
os.system("python food.py")
print "Now don't wait up and get something to eat"

我正在尝试匹配一个字符串说“古家”,然后执行一个与之关联的特定shell脚本但是无论我输入它总是运行第一个循环我试着评论出“顾家”案例但是它也只是继续在顶部调用if / elseif循环

2 个答案:

答案 0 :(得分:0)

show=raw_input("What do you want to see? \n")
while 1:
    if show in ('Gujia', 'gujia'): 
        call(['bash','gujia.sh'])
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)
        break

尝试上面的方法它会起作用,

如果显示('words','...','....','...') 如果匹配其中一个执行代码,它将搜索单词。

答案 1 :(得分:0)

您似乎犯了几个错误,如下所示。

    我认为
  1. show=raw_input("What do you want to see? \n")需要在while 1循环中。
  2. 对于字符串比较,您可能遗憾地使用==
  3. 实际上,
  4. show is 'Gujia' or 'gujia'(show is 'Gujia') or ('gujia')。并且'guija'不是空字符串,它总是正确的。
  5. 有很多elif input ...,但也许'输入'是'show',不是吗?
  6. 所以,把这些条件放在一起:

    while 1:
        show=raw_input("What do you want to see? \n")
        show = show.lower() # translate strings to lower case.
        if show == 'gujia': 
            call(['bash','gujia.sh'])
            termios.tcflush(sys.stdin, termios.TCIOFLUSH)
    #   subprocess.call(["gujia.sh"])
        elif show == 'dal bati churma':
            call(['bash','dbc.sh'])
        ...