admin管理员组

文章数量:1336087

In my program, I have to enter a string (example banana) and then another string whose place value I want to check (example a).

def placevalue(a,b):
    for i in range(len(a)):
        if a[i]==b:
            return i+1
    else:
        return "not found"
str=input()
s=input()
print(placevalue(str,s)

I tried this code, but it only shows the first-place value (example 2 for a of banana). I want all the place values (example 2,4,6).

In my program, I have to enter a string (example banana) and then another string whose place value I want to check (example a).

def placevalue(a,b):
    for i in range(len(a)):
        if a[i]==b:
            return i+1
    else:
        return "not found"
str=input()
s=input()
print(placevalue(str,s)

I tried this code, but it only shows the first-place value (example 2 for a of banana). I want all the place values (example 2,4,6).

Share asked Nov 19, 2024 at 19:17 Kriti PandeyKriti Pandey 12 bronze badges 2
  • is b a string or a single character? perhaps a few more examples – Jason Harrison Commented Nov 22, 2024 at 1:21
  • instead of for i in range(len(a)) you can use for i, a_letter in enumerate(a). a_letter is a[i] – Jason Harrison Commented Nov 22, 2024 at 1:22
Add a comment  | 

3 Answers 3

Reset to default 1

Your for loop will stop once it hits a return. You'll need to keep track of the values and return those after the loop.

Lets use a list to keep track of the values, and check if the list is empty when we're showing the result.

Then we'll show the result with a f string, or show "not found"

def placevalue(a,b):
    matches = []

    for i in range(len(a)):
        if a[i]==b:
            matches.append(i + 1)

    return matches


str=input()
s=input()

result = placevalue(str,s)
if not result:
    print("not found")
else:
    print(f"Result: {result}")

When running with banana and a, the output is:

Result: [2, 4, 6]

Try it online!

Try this:

def placevalue(a, b):
    positions = []
    for i in range(len(a)):
        if a[i] == b:
            positions.append(i + 1)  # Add the position to the list
    return positions if positions else "not found"  # Return "not found" if the list is empty

string = input("Enter the string: ")
char = input("Enter the character to find: ")
print(placevalue(string, char))

an error you are getting is because return ending function so if call return nothing more of this function will be run, for example.

def return_some_numbers():
   print("1")
   print("2")
   print("3")
   return
   print("4")
   print("5")

will return you:

1 2 3

If you want print all of the index of strings that in the second strings you can do it like that.

def placevalue(a,b):
    list_of_indexes = []
    for i in range(len(a)):
        if a[i]==b:
            list_of_indexes.append(i+1)
    else:
        pass
    return list_of_indexes
st=input()
s=input()
for value in placevalue(sr,s):
    print(value)

or faster

a=input()
b=input()
places_values = [ i+1 for i in range(len(a)) if a == b]
print(f"{value}," for value in places_values)

本文标签: pythonIs there any way to print all the place values instead of only the first oneStack Overflow