0

I created a program where you enter in a word, and certain text is added before each vowel in the inputted word. The program will ask the user to play again until a 'n' for no is entered.

However, when a 'y' is entered it keeps playing, but what happens is this:

First time running the program:

Enter first syllable: ip
Enter second syllable: *zz
Enter word to translate: gibberish
Your translated word is: gipibbezzerizzish
Would you like to play again(y/n)? y
Enter first syllable: ip
Enter second syllable: *zz
Enter word to translate: gibberish
Your translated word is: gizzibbezzerizzish
Would you like to play again(y/n)? 

The first result, "gipibbezzerizzish" is correct. The second time it runs, there is an error, it results "gizzibbezzerizzish" which is incorrect.

At the end of the loop what I did is make final_str = "" so when it starts backup, its empty, but for some reason this doesn't work? What's wrong here?

I'm using Python 3.2.3.

Code:

vowels = "aeiou"
playagain = ""
wildcard = '*'
final_str = ""
vowelcount = 0
first_vowel_count = True
second_vowel_count = False

while playagain != "n": ## If playagain is not no, then keep going.
    first_syl = input('Enter first syllable: ')
    second_syl = input('Enter second syllable: ')
    word = input('Enter word to translate: ')
    for ch in word: ## Run loop for all characters in the entered word.
        if ch.lower() not in vowels: ## Checks if ch is vowel or not in word.
            first_vowel_count = True
            vowelcount += 1
        elif wildcard in first_syl and vowelcount <=2: ## Checks for first wildcard.
            final_str += ch + first_syl[1::] ## For first wildcard, remove * and add the vowel (ch) and the first_syl.
            first_vowel_count = False
            second_vowel_count = True
        elif first_vowel_count and vowelcount <= 2: ## If there is no wildcard, but a vowel, run this loop.
            final_str += first_syl       
            first_vowel_count = False
            second_vowel_count = True
        elif wildcard in second_syl: ## For second wildcard, remove * and add the vowel (ch) and the first_syl.
            final_str += ch + second_syl[1::]     
            first_vowel_count = False
            second_vowel_count = True
        elif second_vowel_count: ## If there is no wildcard, but a vowel, run this loop.
            final_str += second_syl
            second_vowel_count == False
        final_str += ch ## Finally, this is the resulting string to be printed.
    if playagain != "n": ## Ask user to play again.
        print("Your translated word is:", final_str) ## Print the word that has just been translated.
        playagain = input("Would you like to play again(y/n)? ") ## Then ask user to play again.
        final_str = "" ## Reset the string if playagain = "y" and start from the top.
4

2 回答 2

2

There are a bunch of variables that aren't being reset inside your loop: vowelcount, first_vowel_count, and second_vowel_count.

于 2012-09-25T02:56:46.353 回答
2

Having that code in an if statement at the bottom is unnecessary, you wouldn't be in the while loop if playagain were 'n'.

Anyway, final_str definitely gets reset there- but you also need to reset vowelcount, first_vowel_count, and second_vowel_count. You're probably better off just setting those at the beginning of the loop so as to be DRY.

于 2012-09-25T02:59:07.613 回答