Code not giving any output


#to trace for presence of an integer in a string

#pos@param to track index
#count@param to track integer
#digits a list
#s is a string
pos = 0
digits = [0,1,2,3,4,5,6,7,8,9]


# skip spaces and remove "+" and "-"


def is_integer(s,count):
    s.strip(" ")
    s.strip("+")
    s.strip("-")

# to identify digits at each index and determine if its in the list
    while pos < len(s):
        if pos == digits [0:]:
            count += 1
            print ("the number of digits are", count)

    return count


print (is_integer("space 56", 0))


Right here, this is essentially checking if pos is equal to the array digits, which it never can be as pos is an integer. Also, the condition checks if pos < len(s), however pos is not incremented anywhere inside of the while loop, and as such this condition will never be met, and you’ll get an infinite loop. Fix these two issues and your code should work.

I want it to check against each element in the list

in that case you would be better using

if pos in digits:
  # code here

I think there may still be a couple of issues after doing this, but give it a go and see if you can work from there!

Please try to write this code

for digit in digits:

        if isinstance(digit, int):

            count += 1

            print(f"Index of {digit} the number of digits are {count}")

Please dont use infinite loop. It will destroy your computer.

That while loop does look like a problem to me. pos is not modified anywhere, so it would remain 0 and the loop would never end.
You can iterate through a string using a for loop, the following is an example of this if you need a reminder.

def string_iteration(string):
  for letter in string:
    print(letter)
  print("-------------------")
  for num in range(len(string)):
    print(num, string[num])

string_iteration("Hello")

#to trace for presence of an integer in a string

#pos@param to track index
#count@param to track integer
#digits a list
#s is a string
pos = 0
digits = [0,1,2,3,4,5,6,7,8,9]


# skip spaces and remove "+" and "-"


def is_integer(s,count):
    s.strip(" ")
    s.strip("+")
    s.strip("-")

# to identify digits at each index and determine if its in the list
    while pos < len(s):
        if pos in digits [0:len(s)]:
            count += 1
            pos += 1
            print ("the number of digits are", count)

    return count

print(is_integer("space 56", 0))


am getting this error now

Almost there! Python can be a bit strange with interpreting global variables and so you have to be careful when using them. When assigning a value to pos inside of the function, it is trying to effectively create a new local variable called pos, and amend that. However since amending is part of the creation, it thinks you are amending a variable that has not been declared yet and throws an error.

You have a couple of ways to fix this. If you want to use the global variable for whatever reason, you can pass it in as a parameter like so:

def is_integer(s, count, pos)

and then pass the value 0 in. However, it isn’t necessary to pass in a value that will never change, so alternatively at the beginning of your def you can just use the global keyword.

def is_integer(s, count):
  global pos

Your third option, and in my opinion the most consistent option to keep your code clean and bug free, is to move the declaration of pos inside of the function definition. In some cases pos may be needed for multiple functions and global scope is required, however here it would be much better as a local variable. So you would remove pos = 0 from the top, and move it inside of the function declaration like so:

def is_integer(s, count):
  pos = 0

This keeps the variable contained to this function, and means that it will fix your function, and not pollute the environment. Whichever solution you choose is up to you for your needs, but these are your options. Your code should work when you implement any of the above solutions.

1 Like

Thank you that corrected the code but l didn’t get the desired output,


#to trace for presence of an integer in a string


#count@param to track integer
#digits a list
#s is a string

digits = [0,1,2,3,4,5,6,7,8,9]


# skip spaces and remove "+" and "-"


def is_integer(s,count):
    global pos
    s.strip(" ")
    s.strip("+")
    s.strip("-")

# to identify digits at each index and determine if its in the list

    for letters in s:
        if letters in digits[0:10]:
            count += 1

        print ("the number of digits are", count)

    return count

print(is_integer("space 56", 0))

this line is not doing what l want it to do, its not checking each letter against the list of digits to see if there are integers and then if recognised then stored in the count

I also read an article on global and local variable. thanks for the knowledge

I am trying to check if from a given string there are any integers, this is checking the digits list, my purpose of the digit list was to check the string against it to spot digits

Consider:

print(5 == '5') print('5' in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Actually, it is. You also don’t need to create a copy of the entire list with the slice method:

digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(digits)
print(digits[0:10]) #this makes a copy of the whole list, and then prints it.
2 Likes

#to trace for presence of an integer in a string


#count@param to track integer
#digits a list
#s is a string

digits = [0,1,2,3,4,5,6,7,8,9]


# skip spaces and remove "+" and "-"


def is_integer(s,count):

    s.strip(" ")
    s.strip("+")
    s.strip("-")

# to identify digits at each index and determine if its in the list

    for letters in s:
        if letters in digits:
            count += 1

        print ("the number of digits are", count)

    return count

print(is_integer("space 56", 0))


this is how it looks like

Please try to use isinstance(object, type) method

s = "space 56"

for letters in s:
    if isinstance(s,int):
        print("true")
        

this is also not giving me the result

Please put the object as a parameter.

for letters in s:
    if isinstance(letters,int):
        print("true")

like that

I will check this code later because I am super busy right now.