【问题标题】:"TypeError: 'function' object is not subscriptable" on a string?字符串上的“TypeError:'function'对象不可下标”?
【发布时间】:2018-05-02 13:48:06
【问题描述】:

我正在编写一个小游戏来猜测一个数字,但我不明白为什么会出现此错误,因为我的 numCheck() 函数中的 num 和 userNum 都应该是字符串而不是函数。

import random

def num():
    """Returns a number between 1000 and 9999."""
    num = str(random.randint(1000, 9999))
    print("Random number is: " + num) #for testing purposes
    return num

def userNum():
    """Asks user for a number between 1000 and 9999."""
    while True:
        try:
            userNum = int(input("Choose a number between 1000 and 9999: "))
        except ValueError:
            print("This isn't a number, try again.")
            continue
        if userNum < 1000 or userNum > 9990:
            print("Your number isn't between 1000 and 9999, try again.")
            continue
        else:
            break
    userNum = str(userNum)
    return userNum

def numCheck(num, userNum):
    """Checks the number of cows and bulls."""
    cow = 0
    bull = 0
    for i in range(0, 3):
        if userNum[i] == num[i]:
            cow += 1
        else:
            bull += 1
    print("You have " + str(cow) + " cow(s) and you have " + str(bull) + " bull(s).")
    return cow

def gameLoop():
    """Loops the game until the user find the right number."""
    num()
    cow = 0
    if cow < 4:
        userNum()
        numCheck(num, userNum)
    else:
        print("Congratulation, You found the right number!")

gameLoop()

我在运行脚本时遇到的错误如下:

==================== RESTART: /home/pi/Desktop/cowbull.py ====================
Random number is: 8104
Choose a number between 1000 and 9999: 5555
Traceback (most recent call last):
  File "/home/pi/Desktop/cowbull.py", line 47, in <module>
    gameLoop()
  File "/home/pi/Desktop/cowbull.py", line 43, in gameLoop
    numCheck(num, userNum)
  File "/home/pi/Desktop/cowbull.py", line 30, in numCheck
    if userNum[i] == num[i]:
TypeError: 'function' object is not subscriptable
>>> 

请注意,其他事情目前可能无法正常工作或完美,但我只是想弄清楚这个错误的逻辑,以便我可以继续。

感谢您的帮助!

【问题讨论】:

  • 您有一个名为 userNum 的函数,并且您还使用相同的名称作为函数 numCheck 的参数。总而言之,这是你的问题。解决方案:更改其中一个名称。

标签: python string python-3.x function typeerror


【解决方案1】:

这是你的函数gameLoop

def gameLoop():
    """Loops the game until the user find the right number."""
    num()
    cow = 0
    if cow < 4:
        userNum()
        numCheck(num, userNum)
    else:
        print("Congratulation, You found the right number!")

您调用了一个名为userNum() 的函数,但您没有将其返回值分配给变量。在下一行中,您将userNum 作为参数传递给函数numCheck。由于userNum 在前一行中是一个函数,所以它现在必须仍然是一个函数(在 Python 中将函数作为参数传递给另一个函数是完全合法的)。

在上一行中,您需要将userNum 的返回值分配给一个新变量。然后将该变量传递给numCheck

        x = userNum()
        numCheck(num, x)

您对函数num 犯了完全相同的错误。即使您希望 numuserNum 是字符串,实际上它们都是函数。这两个函数都返回一个字符串,但是您需要将返回的值分配给新变量以便以后使用它们。

Abdou 的评论是正确的,使用相同的变量名来表示不同的东西会让人困惑。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2016-07-20
    • 2020-07-28
    • 1970-01-01
    • 2016-07-31
    • 2021-07-16
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多