Function to return the index at which a specific letter occurs always returns nothing regardless of input in Python?

amer_413

I'm trying to write a code that will return the index at which the letter c first shows up, or None if c isn't in the string. Here's what I've come up with so far:

def find_c(f):
    z = 0
    c_pos = None
    while c_pos is None or z < len(f):
        if f[z] == 'c' or f[z] == 'C':
            c_pos = z
            z += 1
        return c_pos

But regardless of what string I input my output is always None. Any help?

Karl

If you are still wanting to use the while loop, after you find the index of the first 'C' you probably just want to return that index instead of looping through the rest of the string.

def find_c(f):
    z = 0
    while z < len(f):
        if f[z] == 'c' or f[z] == 'C':
            return z # Returns first index where it finds 'c'
        z += 1
    return None # Return None if never found

However an easier way to do this instead of using a while loop would be to use a for loop enumerating through the values. You can also use the .lower() function to make the characters lowercase so you only have to compare against 'c' like the other commmenter mentioned.

Using a for loop instead:

def find_c(f):
    for index, char in enumerate(f):
        if char.lower() == 'c':
            return index # Returns first index where it finds 'c'
    return None # Return None if never found (reached end of string)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python - Function always returns the same output regardless of input, how can I fix this?

Return function returns nothing

python function returns nothing

Test a function in Python which merges files and returns nothing

Python function that returns the first location as index in which the strings differ. If the strings are identical, it should return -1

Replace function $$find always returns nothing

algorithm - Searching for an input for which a function returns a specific value

importing and calling a function in Typsescript which returns nothing

How to elegantly create a function which returns a lambda's return value or nothing?

array of strings always return the last element regardless of the index number

How to include an input argument in function which is not always required (in python)?

Upper case letter input in scrabble game always returns 0

Python code returns nothing after putting input

Recursive function returns undefined regardless of enough return statements

Python if condition always evaluates to false regardless of user input

Code always doing the same path regardless of input. (PYTHON)

Testing a function which returns an object that returns a function which return a boolean

Can I create a shortcut which points to a specific drive regardless of its drive letter?

JS Checking return function with if, always returns false

Is there a way to force Python function defined under a class to return something (as opposed to nothing) of specific data type?

What is the return type of a function which returns itself?

How to return a specific value when if a value never occurs in an array function?

Is it possible to bind a function which returns an observable to an @Input?

return function in another child class from the same parent returns nothing

Test if function returns nothing powershell and return "No Results found"

Post method always returns nothing

Python function returns only index

Function which return html from an input string

Why scanf() always return nothing?