wannabedon
wannabedon

Reputation: 47

I couldn't find any solutions for my TypeError: object of type 'bool' has no len() error

In this code below here, i wanted to create a guessing game. Player enters 4 numbers and the computer tells the player how many numbers are correct. But when i run the program, just after I input a number it gives me "TypeError: object of type 'bool' has no len()" error. I've read about this error but I couldn't find any solutions. Any help to solve? And how to solve please?

import random

numbers = int(random.randrange(1000,9999))
nums = [int(x) for x in str(numbers)]

while True:
    try:    
        guess = int(input('Enter your guess: '))
        mynumbers = [int(k) for k in str(guess)]
        i = 0

        if int(guess) == int(numbers):
            print('Congragulations, you have guessed the number!')
            print('It took [] tries to guess')
            break
        else:
            a = len((mynumbers) in (nums))
            print ('*' * a)

            i+=1
            continue
    except ValueError:
        print('Please enter a number.')
        continue

Upvotes: 1

Views: 4740

Answers (3)

Wong Siwei
Wong Siwei

Reputation: 113

you need a for loop in your else statement.

...

else:
    number_of_rights = 0
    for index in mynumbers:
        if index in nums:
            number_of_rights += 1
    print ('*' * number_of_rights)

...

Upvotes: 0

Stonecutter
Stonecutter

Reputation: 131

The statement

((mynumbers) in (nums))

delivers True or False -> Here you can't use the len()-command

edit: The solution should be anything like this:

counter=0
for i in mynumbers:
     if i in nums:
         print i
         counter+=1
print counter

The counter should be your result then

Upvotes: 0

R.yan
R.yan

Reputation: 2382

Since ((mynumbers) in (nums)) will return Boolean so cannot call len()

For what you want to do

try:

a = sum([x in nums for x in mynumbers])

It will count number of digits you guess correctly.

Upvotes: 1

Related Questions