Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果循环内部为循环,则表示循环工作不正常_Python_Loops_For Loop_If Statement_While Loop - Fatal编程技术网

Python 如果循环内部为循环,则表示循环工作不正常

Python 如果循环内部为循环,则表示循环工作不正常,python,loops,for-loop,if-statement,while-loop,Python,Loops,For Loop,If Statement,While Loop,我有一个for循环,首先遍历数字列表的长度范围 之后,我有一个if循环,检查每个数字是否小于或等于一个变量的长度 当输入单个数字(userLetterLocation)时,这些循环不会出现问题。然而,当我输入多个内容,如“2和6”或“3,6”,if循环识别出一个较大的数字并显示“ERROR”,但它随后中断while循环并继续,这显然会中断程序 我相信if循环仍在更新keep_running with False,这会停止while循环,但我不确定为什么 def letterLocation(us

我有一个for循环,首先遍历数字列表的长度范围

之后,我有一个if循环,检查每个数字是否小于或等于一个变量的长度

当输入单个数字(userLetterLocation)时,这些循环不会出现问题。然而,当我输入多个内容,如“2和6”或“3,6”,if循环识别出一个较大的数字并显示“ERROR”,但它随后中断while循环并继续,这显然会中断程序

我相信if循环仍在更新keep_running with False,这会停止while循环,但我不确定为什么

def letterLocation(userWordHidden, compChoice):
    keep_running = True
    while(keep_running):
        userLetterLocation = input('Where does the letter(s) occur \
in the word?: ')
        letterPosition = re.findall("\d", userLetterLocation)
        letterPosition = [int(i) for i in letterPosition]
        print(len(letterPosition))

        for i in range(len(letterPosition)):
            if letterPosition[i] <= len(userWordHidden):
                keep_running = False
            else:
                print("ERROR")

    for i in letterPosition:
        userWordHidden[i-1] = compChoice

    print("Your Word:", end =" ")
    for i in range(len(userWordHidden)):
        print(userWordHidden[i], end =" ")

    print("\n")

    computerChoice(userWordHidden)
def letterLocation(userWordHidden,compChoice):
保持运行=真
同时(保持运行):
userLetterLocation=input('字母出现在哪里\
在单词中?:')
letterPosition=re.findall(“\d”,userLetterLocation)
letterPosition=[字母位置中i的int(i)]
打印(透镜(字母位置))
对于范围内的i(len(字母位置)):

如果letterPosition[i]欢迎来到StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。您发布的代码无法运行几个未定义的符号,并且没有驱动程序。之后,它将暂停等待输入:我们希望您提供一个内置测试,而不是让我们提供输入。@Prune我不知道您的意思?这是直接从Python粘贴的。你能给出一个可复制的例子吗?我想你要找的是
all()
函数:
if-all(pos)你也可以使用
if-max(letterPosition)
def main():
    computerChoice(wordInput())


def wordInput():
    while(True): #while function to validate user input
        userWord = input("Please choose a word between 5 and 10 letters long: ")
        #if the word entered is 5 > characters > 10 an error is printed and
        #the user is asked to input again
        if len(userWord)<5 or len(userWord)>10:
            print("Error: Word is not between 5 and 10 letters!")
        else:
            break

    #stage 2: printing lines in place of characters to hide the word chosen
    print("")
    userWordHidden = list(userWord)
    #replacing characters from userWord and changing them to dashes in a new
    #variable called userWordHidden
    for i in range(len(userWord)):
        userWordHidden[i] = "-"

    #printing a space between dashes for better visual
    print("Your Word:", end =" ")
    for i in range(len(userWord)):
        print(userWordHidden[i], end =" ")

    print("\n")

    return userWordHidden


def computerChoice(userWordHidden):
    compGuessCount = 0

    while compGuessCount < 6:

        compChoice = random.choice(string.ascii_uppercase)

        while(True):
            compGuess = input('Does your word contain the letter %s? \
Please enter "Yes" or "No": ' % (compChoice))
            if compGuess == "Yes" or compGuess == "No":
                break
            else:
                print('Error: Please enter "Yes" or "No" exactly as typed \n')

        if compGuess == "Yes":
            letterLocation(userWordHidden, compChoice)
        elif compGuess == "No":
            compGuessCount = compGuessCount + 1
            scaffold(compGuessCount)


    if compGuessCount == 6:
        scaffold(compGuessCount)
        winner(compGuessCount)

def letterLocation(userWordHidden, compChoice):
    keep_running = True
    while(keep_running):
        userLetterLocation = input('Where does the letter(s) occur \
in the word?: ')
        letterPosition = re.findall("\d", userLetterLocation)
        letterPosition = [int(i) for i in letterPosition]
        print(len(letterPosition))

        for i in range(len(letterPosition)):
            if np.all(letterPosition) <= len(userWordHidden):
                keep_running = False
            else:
                print("ERROR")

    for i in letterPosition:
        userWordHidden[i-1] = compChoice

    print("Your Word:", end =" ")
    for i in range(len(userWordHidden)):
        print(userWordHidden[i], end =" ")

    print("\n")

    computerChoice(userWordHidden)