简体   繁体   English

为每个项目重复相同的用户输入 - python

[英]Repeat same user input for each item - python

I am in the process of writing a KWIC search engine.我正在编写一个 KWIC 搜索引擎。 I want to let the user decide how many words should be displayed before and after the hit word (range).我想让用户决定在命中词(范围)之前和之后应该显示多少个词。 I have managed this so far, but the range is asked for again after every single hit.到目前为止,我已经做到了这一点,但每次击球后都会再次要求范围。 Do you know how I can let the user select the range only once, and then it will be used for all hits?你知道我怎样才能让用户 select 范围只有一次,然后它将用于所有命中? Here is the part it is about:这是它的部分:

def displayKWIC (text, wordPos):

    global textSequence
    resultline = ''
    ranges =(int(input("Bitte geben Sie ein Range: ")))
    for i in range(max(0,wordPos-ranges), min(len(textSequence[text]),wordPos+ranges)):
        resultline += ' '+ textSequence[text][i]  
    print(text+': =>'+resultline,"\n", "\n")
    return

I assume the problem is somewhere in the ranges line.我认为问题出在范围线的某个地方。

Thanks in advance:)提前致谢:)

The user is asked for ranges after every single hit, since ranges is assigned within displayKWIC .每次点击后都会要求用户提供ranges ,因为rangesdisplayKWIC中分配的。 Because of this, every time displayKWIC (text, wordPos) is called, ranges is assigned anew.因此,每次displayKWIC (text, wordPos)时,都会重新分配ranges

To change this, you could assign ranges outside of displayKWIC and pass it to the function as an argument, alongside text and wordPos .要更改这一点,您可以分配ranges之外的范围,并将其作为参数传递给displayKWIC ,与textwordPos一起传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM