Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Loop Problem
#1
Wall New coder here trying to teach himself.

Look closely at the number of "a" in the next sentence. Why does the following code produce "CCCCCttttt" instead of "CCCCCaaaattttt"?

def anti_vowel(text):
t = ""
vowels = "aeiou"
for i in text:
for vowel in vowels:
if i==vowel:
i=""
else:
t = t+i
return t
print anti_vowel("Cat")
Reply
#2
def anti_vowel(text):
  t = ""
  vowels = "aeiou"
  for i in text:
    for vowel in vowels:
        t = t+i
  return t

print( anti_vowel("Cat") )
Reply
#3
columns are:
1 - Iteration of outer loop
2 - Iteration of inner loop
3 - value of i
4 - value of vowel
5 - value of t

Output:
1        C             1    C    a    C     2    C    e    CC     3    C    I    CCC     4    C    o    CCCC     5    C    u    CCCCC 2        a             1    a    a    CCCCCa     2    a    e    CCCCCaa     3    a    I    CCCCCaaa     4    a    o    CCCCCaaaa     5    a    u    CCCCCaaaaa 3        t             1    t    a    CCCCCaaaaat     2    t    e    CCCCCaaaaatt     3    t    I    CCCCCaaaaattt     4    t    o    CCCCCaaaaatttt     5    t    u    CCCCCaaaaattttt
Reply
#4
Thanks. Nice output.
Reply
#5
I still don't know why the code produced no "a." I thought it would produce 4 "a."

def anti_vowel(text):
   t = ""
   vowels = "aeiou"
   for i in text:
      for vowel in vowels:
         if i==vowel:
            i=""
         else:
            t = t+i
      return t

print anti_vowel("Cat")

The inner loop seems to be stopping as soon as if i == vowel is satisfied. Why doesn't it keep looping until it goes through vowel in vowels?
Reply
#6
You're never using vowel after extracting it.
and 'a' is being printed (with 'Cat' passed to function).
see my output.
so where you say:
      for vowel in vowels:
you could have used
for x in range(5)
and got the same output
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Problem Benno2805 1 582 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Big O runtime nested for loop and append yarinsh 4 1,396 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,604 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,577 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Problem with nested JSON Kalet 7 2,804 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  How do I add another loop to my nested loop greenpine 11 4,576 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,241 Nov-25-2020, 10:04 AM
Last Post: palladium
  Infinite loop problem Zirconyl 5 3,000 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Nested function problem chipx 8 3,500 Oct-21-2020, 11:56 PM
Last Post: jefsummers
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,181 Aug-28-2020, 02:41 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020