0

I'm scripting a small game to guess a number and I don't understand why I'm getting this error as both num and userNum in my numCheck() function should be strings and not functions.

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()

The error I get when I run the script is the following:

==================== 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
>>> 

Note that other things may not be working or perfect at the moment, but I'm only trying to figure out the logic of this error so I can continue on.

Thanks for the help!

1
  • 2
    You have a function named userNum and you are also using that same name as a parameter for the function numCheck. That, in sum, is your issue. Solution: change one of those names. Commented Nov 19, 2017 at 3:30

1 Answer 1

1

Here is your function 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!")

You call a function named userNum() but you don't assign its returned value to a variable. In the next line you pass userNum as an argument to the function numCheck. Since userNum was a function in the previous line, it must still be a function now (it's perfectly legitimate in Python to pass a function as an argument to another function).

In the previous line, you need to assign the returned value from userNum to a new variable. Then pass that variable to numCheck:

        x = userNum()
        numCheck(num, x)

You have made exactly the same mistake with the function num. Even though you want num and userNum to be strings, in fact both of them are functions. Both functions return a string, but you need to assign the returned values to new variables in order to use them later.

Abdou's comment is correct that it's confusing to use the same variable name to mean different things.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.