Python &引用;类型错误:';功能';对象不可下标";在绳子上?

Python &引用;类型错误:';功能';对象不可下标";在绳子上?,python,string,python-3.x,function,typeerror,Python,String,Python 3.x,Function,Typeerror,我正在编写一个小游戏来猜一个数字,我不明白为什么会出现这个错误,因为我的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 purpose

我正在编写一个小游戏来猜一个数字,我不明白为什么会出现这个错误,因为我的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()
随机导入
def num():
“”“返回一个介于1000和9999之间的数字。”“”
num=str(random.randint(10009999))
打印(“随机数为:+num)#用于测试目的
返回数
def userNum():
“”“要求用户输入1000到9999之间的数字。”“”
尽管如此:
尝试:
userNum=int(输入(“选择1000到9999之间的数字:”)
除值错误外:
打印(“这不是数字,请重试。”)
持续
如果userNum<1000或userNum>9990:
打印(“您的号码不在1000到9999之间,请重试。”)
持续
其他:
打破
userNum=str(userNum)
返回userNum
def numCheck(num,userNum):
“”“检查奶牛和公牛的数量。”“”
奶牛=0
布尔=0
对于范围(0,3)内的i:
如果userNum[i]==num[i]:
奶牛+=1
其他:
牛市+=1
打印(“您有“+str(牛)+”牛,您有“+str(牛)+”牛)
回归母牛
def gameLoop():
“”“循环游戏,直到用户找到正确的号码。”“”
num()
奶牛=0
如果奶牛小于4头:
userNum()
numCheck(num,userNum)
其他:
打印(“恭喜,你找到了正确的号码!”)
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
>>> 
======================重新启动:/home/pi/Desktop/cowbull.py====================
随机数为:8104
选择介于1000和9999之间的数字:5555
回溯(最近一次呼叫最后一次):
文件“/home/pi/Desktop/cowbull.py”,第47行,在
gameLoop()
文件“/home/pi/Desktop/cowbull.py”,第43行,在gameLoop中
numCheck(num,userNum)
文件“/home/pi/Desktop/cowbull.py”,第30行,numCheck格式
如果userNum[i]==num[i]:
TypeError:“函数”对象不可下标
>>> 
请注意,目前其他事情可能不起作用或不完美,但我只是试图找出这个错误的逻辑,以便继续


谢谢你的帮助

这是您的函数
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!")
您在函数
num
中犯了完全相同的错误。尽管您希望
num
userNum
都是字符串,但实际上它们都是函数。这两个函数都返回一个字符串,但您需要将返回的值分配给新变量,以便以后使用它们


Abdou的评论是正确的,即使用相同的变量名表示不同的内容是令人困惑的。

您有一个名为
userNum
的函数,并且您还使用该名称作为函数
numCheck
的参数。总之,这是你的问题。解决方案:更改其中一个名称。
        x = userNum()
        numCheck(num, x)