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 |3 Answers
Reset to default 1Your 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
版权声明:本文标题:python - Is there any way to print all the place values instead of only the first one? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403859a2468419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for i in range(len(a))
you can usefor i, a_letter in enumerate(a)
. a_letter is a[i] – Jason Harrison Commented Nov 22, 2024 at 1:22