Python code problem

Python code problem
0

Tell us what’s happening:
iam writing a code in python which replaces the vowel letters in user’s word with “g”
but the code has some error…
pls find out and tell me pls…

Your code so far

print("giraffe language")
def giraffe_lang():
  word=input("enter the word:")
  i=0
  j=0
  vowels=['a','e','i','o','u']
  while i<len(word):
    while j<5:
        if word[i]==vowels[j]:
            word[i]=vowels[j]
            j=5
  print("your word is"+word)
giraffe_lang() 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/responsive-web-design/responsive-web-design-projects/build-a-product-landing-page

First of all you are not incrementing i or j so you are not moving anywhere in scanning the given word,
But I’d propose a better solution to this problem:

print("giraffe language")

def giraffe_lang():
    word = list(input("Enter a word: ").lower())
    vowels = ('a', 'e', 'i', 'o', 'u')
    for i in range(len(word)):
        if word[i] in vowels:
            word[i] = 'g'
    print("your word is "+"".join(word).capitalize())

giraffe_lang()
print("giraffe language")
def giraffe_lang():
  word=input("enter the word:")
  i=0
  j=0
  vowels=['a','e','i','o','u']
  while i<len(word):
    while j<5:
        if word[i]==vowels[j]:
            word[i]=vowels[j]
            j=5
        j=j+1
    i=i+1
  print("your word is"+word)
giraffe_lang()

i’ve added the incrementation… but still the program goes error

You cannot modify strings in Python,
In order to change the specific char - word[i]=vowels[j]
You must convert the string into a list - word = list(input("Enter a word: ").lower())
And when you print convert the list into a string - print("your word is "+"".join(word).capitalize())

look at this snippet.
ignoring syntax and logic errors for a moment, the letter g is never used in your code so I would say it is impossible to change the vowels to g if you never use the g in your code

My personal suggestion, this is the point to move to regular expressions. While they may not be the fastest solution in Python circumstances, I think it’s an important aspect to learn and master-- also it’s designed for pretty much exactly what you want to do.

Python regular expressions (the re module in Python) has as method re.sub() for substituting, or replacing, regular expression matches with some other character set. Pretty much exactly what you appear to need (I think).

import re
phrase = "The rain in Spain"
altered_phrase = re.sub("[aeiou]", "g", phrase)  # replace any a, e, i, o, or u in phrase with g
print(altered_phrase)
# Thg rggn gn Spggn

Explanation

You are currently using a while loop in your code…well 2 actually. Specifically, the while i<len(word) portion is creating an infinte loop that never ends, no bueno.
While loops are useful for a lot of situations, but (in my opinion) it should be a last resort choice of loop, because of its risk of never ending the loop.
The variable i is never changed within your loop, it will never be incremented to 5, and therefore will never satisfy the end condition of this loop… Infinite Looping ensues. Using the regular expression example above, an additional while loop(s) isn’t required, so hurrah. But if you want to do this the non-regular expression way, that’s possible too! And easy.

Additionally, I’d like to point out that in Python, the non-regular expression way can be accomplished without any while loops at all, because for loops will also work. for loops are often better choices in Python than while loops if possible (especially for any beginners) because even if things don’t go as planned, the loop will still usually end eventually.

Consider this:

word = 'test'
i = 0
while i<len(word):
    print(i)
# infinitly prints 0 and hopefully crashes magnificently

This is a problem, because i is never incremented to satisfy the end conditions of the while loop. Compared to:

for x in range(len(word)):  #evaluates to: for x in [0, 1, 2, 3]
    print(x)
# I'm using x instead of i here as the iterable name, because the name "i" is often
# reserved for an index value, rather than the actual lookup value (x, in this case)
# prints 0, 1, 2, 3

The second example always ends and requires no explicit variable manipulation ( of i) to accomplish that. When working in Python and constructing a while loop I always ask myself if a while loop or a for loop is more useful in my circumstances. Often, but not always, a for loop is actually what I’m after.

An example non-regular expression way of doing this with no while loops or list conversion (done with Python strings), and lots of comments:

def giraffe_lang():
    new_word = ""  # start as an empty string
    word = input("Enter A Word: ").lower().strip()  # removes preceding/trailing spaces
    vowels = "aeiou"  # a list or set isn't needed as strings are static but iterable in Python! Yay!
    # This means 'vowels' can be treated one-character-at-a-time like a list, without needing conversion
    # weird, from many other language standpoints, but it simplifies what follows
    for letter in word:  # loop through characters (letters) in word
        if letter in vowels:  # Can be thought of as: if letter in ['a', 'e', 'i', 'o', 'u']
            letter = "g"  # within scope (important), any vowel is changed to 'g'
        new_word += letter
    to_print = f"your word is {new_word.upper()}"
    print(to_print)
    return to_print

    """ <string>.upper() is the Python string method for upper-case conversion.
     f-string format only works with newer Python, but is definitely the easiest way to deal with
     string formatting on newer Python versions so I included it. Use other formatting
     methods for other Python versions.
    ex_2: print("your word is {}".format(new_word.upper()))
        or
        print "your word is %s" % new_word.upper()      (Python 2)
    """

Edit:
An (ugly, I think) one-liner you could also choose to use that utilizes Python list comprehensions.

def girrafe_lang():
    print('your word is ' + ''.join(x.upper() if x not in 'AEIOUaeiou' else 'G' for x in input('Enter A Word: ').strip()))