I was given a question with a random string such as
example = ‘asdkfkebansmvajandnrnndklqjjsustjwnwn’
and was asked to find the number of a’s in this string with a while and with a for loop
So simply using the count() function like this is not allowed:
print('# of a:’, example.count(‘a’))
We were given one example: (and were told to find a different way)
counter = 0
for letter in example:
if letter == ‘a’:
counter = counter 1
print(counter)
I’m very new to python and I really can’t find a way. I thought of converting this string into a list that contains every character as a different object like this:
example_list = list(example)
but then I still couldn't find a way.
We were given two starting points, so the end code has to be in a somewhat similar format and we're not really allowed to use more advanced functions (simple string or list functions and if-statements are allowed as far as I know).
For while-loop:
counter = 0
while counter < 4:
print(example_list[counter])
counter = 1
And for for-loop:
for counter in range(0, len(example_list)):
print(counter, example[counter])
I either end up printing every single character with its position, or I end up printing the number without actually using the loop.
CodePudding user response:
I think the advises tell you that you have to iterate through array using a counter. Here is the example for while loop:
example = 'asdkfkebansmvajandnrnndklqjjsustjwnwn'
counter = 0
a_count = 0
while counter < len(example):
if example[counter] == 'a':
a_count = 1
counter = 1
print(a_count)
And for for-loop it might look like this:
for counter in range(len(example)):
if example[counter] == 'a':
a_count = 1
CodePudding user response:
Note that converting to a list isn't necessary, since you can iterate over a string exactly the same way that you'd iterate over a string that's been converted into a list.
For your first starting point, I think the idea is to iterate by index:
index = 0
counter = 0
while index < len(example):
if example[index] == 'a':
counter = 1
index = 1
And the for-loop version would be:
counter = 0
for index in range(len(example)):
if example[index] == 'a':
counter = 1
Note: iterating by index like this is actually considered bad practice in Python (because it's basically just adding unnecessary work), and the preferred method is to iterate by value, as in the example you were given and then told not to use.
CodePudding user response:
Two functions, while_count
and for_count
, to achieve what you need:
def while_count(s):
counter, i = 0, 0
while i < len(s):
if s[i] == "a":
counter = 1
i = 1
return counter
def for_count(s):
counter = 0
for i in range(len(s)):
if s[i] == "a":
counter = 1
return counter
You can make the for
case much simpler using a list comprehension:
def for_count2(s):
return sum([x=="a" for x in s])
CodePudding user response:
Here is the solution for your question
1.Using for loop
example = 'asdkfkebansmvajandnrnndklqjjsustjwnwn'
count=0
for i in example:
if i.lower()=='a':
count =1
print(count)
2. Using While loop:
example = 'asdkfkebansmvajandnrnndklqjjsustjwnwn'
loop_count=0
a_counter=0
lis=list(example)
while loop_count<len(example):
if lis[loop_count]=='a':
a_counter =1
loop_count =1
print(a_counter)
Might it help if it does vote my ans up