1

I have a task to make a function that returns True if there's a number (0-9) in word and False if there's no number in word:

def has_digits(string):
    for r in range (0,10)
    if r in string:
        return 'True'
    else: return 'False'

print(has_digits('Python3'))

Generally I have this error message:

TypeError: 'in <string>' requires string as left operand, not int
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Egorsky
  • 179
  • 1
  • 11
  • 4
    Well given that you have `for r in range(0, 10):`, inside the loop `r` will be an `int`. When you do `if r in string:`, therefore, you're going to try to evaluate e.g. `0 in 'Python3'` and, as the error tells you, that doesn't make sense. Also your indentation is broken as posted and they probably want you to return the booleans `True` and `False` not a string that just looks like it. – jonrsharpe Aug 26 '19 at 11:25
  • this is nicer: `return any(char.isdigit() for char in string)` see: https://stackoverflow.com/questions/19859282/check-if-a-string-contains-a-number – Laurens Koppenol Aug 26 '19 at 11:28

6 Answers6

2
def has_digits(string):
    return any(char.isdigit() for char in string)

print(has_digits('abc1'))

Your example should be like this to work:

def has_digits(string):
    b = False
    for r in range(0, 10):
        if str(r) in string:
            b = True
    return b


print(has_digits('Python3'))

You should have parsed str(r) and secondly your function would check if 0 is in your string and return false immediately. Would not run for the rest values in your loop.

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • 2
    SO isn't a code-writing service, and this doesn't explain to the OP why their initial attempt didn't work. – jonrsharpe Aug 26 '19 at 11:28
  • What actually char.isdigit does? And why it doesn't have any varible inside? – Egorsky Aug 26 '19 at 11:29
  • @Balloonbear that information is available in the documentation: https://docs.python.org/3/library/stdtypes.html#str.isdigit – jonrsharpe Aug 26 '19 at 11:30
  • @Balloonbear `char.isdigit()` checks if a character is a number and that runs for every single character in your string. Your example didn't work first of all because you should have parsed `str(r) and secondly your function would check if 0 is in your string and return immediately. Would not run for the rest. I fixed your example too to work. – Kostas Charitidis Aug 26 '19 at 11:31
  • @jonrsharpe is that better? I gave him a working example. I didn't that I had to explain the wrong one too. – Kostas Charitidis Aug 26 '19 at 11:34
  • It still doesn't explain what the problem was. The solution is trivial, yes, but if they just copy-paste it what have they learned (other than that they can just dump their next trivial problem here, too)? – jonrsharpe Aug 26 '19 at 11:35
  • @jonrsharpe I explained in a comment above. Wasn't that sufficient? – Kostas Charitidis Aug 26 '19 at 11:38
  • Comments are ephemeral, useful information should be *in your post*. – jonrsharpe Aug 26 '19 at 11:39
  • @jonrsharpe ok then. – Kostas Charitidis Aug 26 '19 at 11:40
  • One should keep in mind .isdigit() expected behaviour with superscript and subscript: '₄₂'.isdigit() is True. It depends on context whether these should be considered digits or not. Trying to convert these digits into integer with int() will raise ValueError. – Aivar Paalberg Aug 26 '19 at 11:54
1

You have to convert the int object from your for loop to a str object because if a in b only works for a and b as an string (str) object. And you have to remove one return from the loop. Otherwise the method isDigit will end with true or false at the first letter of your string.

def has_digits(string):
    for r in range(0, 10):
        if str(r) in string:
            return true

    return false

print(has_digits("Python3"))

>> true

Now the method will return true if your string contains a digit. Otherwise it will loop further through the string and check the next letters. At the end the method returns false because the string ends and the method doesn´t return with true from the loop.

How does this code works now?
Assume that you pass the string Python3 into the method has_digits then the first for loop iterate over the numbers 0 to 9. So during the first loop r is equal 0. Now the conditional check if str(r) in string will occur. The if statement checks if str(0) ("0" the string version of the number 0) is in the string string (which is your Python3). There is no "0" in it, so the condition is false and the for loop continues with 1. The whole procedure repeat until 3. For 3 the if statement returns true, because "3" is in Python3 and now the method will return true which signal that your string has at least one digit.

Kampi
  • 1,798
  • 18
  • 26
  • This is a quite interesting case. Okay so with str(r) i made thouse "numbers" string format rithg? So the position of the "return 'False" does the thing. I don't quite get the logic of how it works – Egorsky Aug 26 '19 at 11:38
  • Yes. `str(a)` convert the object `a` into a `string` object. Now you have all methods for a `string` object available (like the `if(a in b)` check). I will update my answer to explain the logic. – Kampi Aug 26 '19 at 11:42
0

Change this:

if r in string:

to this:

if str(r) in string:
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Den Zalman
  • 114
  • 1
0

Well, there's a mistake in your code.

def has_digits(string):
    for r in range (0,10)  # here; should have a :(colon) at the end
    if r in string:
        return 'True'
    else: return 'False'

print(has_digits('Python3'))

Correct code should be:

def has_digits(string):
    for r in range(10):
        r = str(r)
        if r in string:
            return True
    return False

print(has_digits('Python3'))
lahsuk
  • 1,134
  • 9
  • 20
0

Reason for the error is you are checking for integer value in string. So change

if r in string

to

if str(r) in string

Also there is no need for else inside the loop as it will return the false if the first character is not a digit so you will not get correct answer, you can change it to something like

def has_digits(string):
    for r in range (0,10):
         if str(r) in string:
             return 'True' # use boolean return for better use
    return false

Though this is correct way and you will get your results but learn about list comprehensions and generators. You will get your results quicker and in better way.

-1

This error arise because you are comparing string with int. change r in string to str(r) in string will resolve this error.

def has_digits(string):
    for r in range (0,10):
        if str(r) in string:
            return 'True'    
    return 'False'

print(has_digits('Python3'))
GIRISH kuniyal
  • 740
  • 1
  • 5
  • 14