Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 - Fatal编程技术网

Python 索引器:字符串索引超出范围?百吉饼游戏

Python 索引器:字符串索引超出范围?百吉饼游戏,python,Python,就像标题一样,它总是显示: 索引器错误:字符串索引超出范围。 谁能帮我修一下吗 clues = [] for i in range(len(guess)): if guess[i] == secretNum[i]: clues.append('Fermi') elif guess[i] in secretNum: clues.append('Pico') ... while guessesTaken <= MAX_GUESS:

就像标题一样,它总是显示:

索引器错误:字符串索引超出范围。 谁能帮我修一下吗

clues = []
for i in range(len(guess)):
    if guess[i] == secretNum[i]:
        clues.append('Fermi')
    elif guess[i] in secretNum:
        clues.append('Pico')

...

while guessesTaken <= MAX_GUESS:
        guess = ''
        while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
            print('Guess #%s: ' % (guessesTaken))
            guess = input()
        clues = getClues(guess, secretNum)
        print(clues)
例外情况:

回溯最近一次呼叫上次: 文件/Users/qiyin/Downloads/Python37/bagels.py,第56行,in 线索=getCluesguess,secretNum 文件/Users/qiyin/Downloads/Python37/bagels.py,第20行,在getClues中 如果猜测[i]==secretNum[i]: 索引器错误:字符串索引超出范围
您在getSecretNum函数中犯了缩进错误,它将始终给您一个长度等于1的秘密数,因此索引超出范围

事实上还有其他几个缩进错误! 我帮你修好了:

import random

NUM_DIGITS = 3
MAX_GUESS = 10


def getSecretNum():
    numbers = list(range(10))
    random.shuffle(numbers)
    secretNum = ''
    for i in range(NUM_DIGITS):
        secretNum += str(numbers[i])
    return secretNum


def getClues(guess, secretNum):
    if guess == secretNum:
        return 'You got it!'

    clues = []
    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            clues.append('Fermi')
        elif guess[i] in secretNum:
            clues.append('Pico')
    if len(clues) == 0:
        return 'Bagels'
    clues.sort()
    return ' '.join(clues)


def isOnlyDigits(num):
    if num == '':
        return False

    for i in num:
        if i not in '0 1 2 3 4 5 6 7 8 9'.split():
            return False
    return True


print('I am thinking of a %s-digit number. Try to guess what it is. ' % (NUM_DIGITS))
print('The clues I give are...')
print('When I say:    That mean:')
print(' Bagels        None of the digits is correct.')
print(' Pico          One digits is correct but in the wrong position.')
print(' Fermi         One digits is correct and in the rigit position.')

while True:
    secretNum = getSecretNum()
    print('I have thought up a number. You have %s guesses to get it.' % (MAX_GUESS))

    guessesTaken = 1
    while guessesTaken <= MAX_GUESS:
        guess = ''
        while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
            print('Guess #%s: ' % (guessesTaken))
            guess = input()
            clues = getClues(guess, secretNum)
            print(clues)
            guessesTaken += 1

        if guess == secretNum:
            break
        if guessesTaken > MAX_GUESS:
            print('You ran out of guess. The answer was %s.' % (secretNum))

    print('Do you want to play again? (yes or no)')
    if not input().lower().startswith('y'):
        break

什么是secretNum?将其和所有相关代码片段发布在问题正文中。评论格式无法阅读。整个订单附在下面。非常感谢你,我帮助了你!周末快乐:D
import random

NUM_DIGITS = 3
MAX_GUESS = 10


def getSecretNum():
    numbers = list(range(10))
    random.shuffle(numbers)
    secretNum = ''
    for i in range(NUM_DIGITS):
        secretNum += str(numbers[i])
    return secretNum


def getClues(guess, secretNum):
    if guess == secretNum:
        return 'You got it!'

    clues = []
    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            clues.append('Fermi')
        elif guess[i] in secretNum:
            clues.append('Pico')
    if len(clues) == 0:
        return 'Bagels'
    clues.sort()
    return ' '.join(clues)


def isOnlyDigits(num):
    if num == '':
        return False

    for i in num:
        if i not in '0 1 2 3 4 5 6 7 8 9'.split():
            return False
    return True


print('I am thinking of a %s-digit number. Try to guess what it is. ' % (NUM_DIGITS))
print('The clues I give are...')
print('When I say:    That mean:')
print(' Bagels        None of the digits is correct.')
print(' Pico          One digits is correct but in the wrong position.')
print(' Fermi         One digits is correct and in the rigit position.')

while True:
    secretNum = getSecretNum()
    print('I have thought up a number. You have %s guesses to get it.' % (MAX_GUESS))

    guessesTaken = 1
    while guessesTaken <= MAX_GUESS:
        guess = ''
        while len(guess) != NUM_DIGITS or not isOnlyDigits(guess):
            print('Guess #%s: ' % (guessesTaken))
            guess = input()
            clues = getClues(guess, secretNum)
            print(clues)
            guessesTaken += 1

        if guess == secretNum:
            break
        if guessesTaken > MAX_GUESS:
            print('You ran out of guess. The answer was %s.' % (secretNum))

    print('Do you want to play again? (yes or no)')
    if not input().lower().startswith('y'):
        break