预期输出错误:循环未正常工作

时间:2016-10-14 01:46:13

标签: python

我仍在努力将第二个'for'变量链接在一起。第一个'for'循环正常工作,但后半部分卡在一个变量上,这不允许它在以后的可重复循环中正常运行。我怎么能更好地写这个,以便文本的功能是全局的,所以变量'xcr'不是本地的。我知道我是初学者,但总是赞赏任何帮助!!谢谢!

sequence = open('sequence.txt').read().replace('\n','')
enzymes = {}
fh = open('enzymes.txt')
print('Restriction Enzyme Counter')
def servx():
    inez = input('Enter a Restricting Enzyme: ')
    for line in fh.readlines():
        (name, site, junk, junk) = line.split()
        enzymes[name] = site 
        global xcr
        xcr = site
        if inez in line:
            print(xcr)
            print('Active Bases:', xcr)
    for lines in sequence.split():
        if xcr in lines:
            bs = (sequence.count(xcr))
            print(bs)
            print('Enzyme', inez, 'appears', bs, 'times in Sequence.')

1 个答案:

答案 0 :(得分:0)

我认为您需要在servy函数内部指定全局而不是外部,但更好的方法是将inez作为参数传递给servx

def servy():
    global inez 
    fh.seek(0); #veryyyyy important
    qust = input('Find another Enzyme? [Yes/No]: ')
    qust = qust.lower()
    if qust == 'yes':
        inez = input('Enter a Restricting Enzyme: ')
        servx()
        servy()
    elif qust == 'no':
        print('Thanks!')
    elif qust not in ('yes', 'no'):
        print('Error, Unknown Command')

或者

def servx(inez):
    . . . 



def servy():
    fh.seek(0); #veryyyyy important
    qust = input('Find another Enzyme? [Yes/No]: ')
    quest = qust.lower()
    if qust == 'yes':
        inez = input('Enter a Restricting Enzyme: ')
        servx(inez)
        servy()
    elif qust == 'no':
        print('Thanks!')
    elif qust not in ('yes', 'no'):
        print('Error, Unknown Command')