2

I am getting the following error

'TypeError: must be str, not generator'.

for the code below

a = '0123456789 01234 01234567890 6789012345 012322456789'
b = a.split(" ")
for num in b:
    if num.count(**i for i in range(10)**) <= 1:
        print("True")
    else:
        print("False")

This above should check whether each number has any duplicated number (False) or not (True).

For instance, the first number 0123456789 returns True because it doesn't have any duplicated number.

On the other hand, the last number 012322456789 returns False because it has three of '2'.

1 Answer 1

2

You can't use .count() with multiple arguments. Change your condition to:

if all(num.count(str(i)) <= 1 for i in range(10)):

This will test each digit against the string and return True if all of them are repeated 1 or 0 times.

You can also only test for the digits in num instead of using every digit from 0 to 9:

if all(num.count(i) == 1 for i in num):

or use set to test for uniqueness:

if len(set(num)) == len(num):
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.