字符串赢了';t在循环结束时正确重置-Python
我创建了一个程序,在其中输入一个单词,并在输入单词的每个元音之前添加某些文本。程序将要求用户再次播放,直到输入“n”表示“否” 但是,当输入“y”时,它会继续播放,但会发生以下情况: 第一次运行程序时:字符串赢了';t在循环结束时正确重置-Python,python,string,loops,Python,String,Loops,我创建了一个程序,在其中输入一个单词,并在输入单词的每个元音之前添加某些文本。程序将要求用户再次播放,直到输入“n”表示“否” 但是,当输入“y”时,它会继续播放,但会发生以下情况: 第一次运行程序时: Enter first syllable: ip Enter second syllable: *zz Enter word to translate: gibberish Your translated word is: gipibbezzerizzish Would you like to
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)?
第一个结果“Gipibbezerizzish”是正确的。第二次运行时,出现错误,结果是“gizzibbezzerizzish”,这是不正确的
在循环结束时,我做的是将final_str=“”,因此当它启动备份时,它是空的,但由于某种原因,这不起作用?这里怎么了
我正在使用Python 3.2.3
代码:
元音=“aeiou”
再次播放=“”
通配符='*'
最终_str=“”
元音计数=0
第一个\u元音\u计数=真
第二个元音\u计数=假
再玩一次!=“n”:##如果playreach不是no,那么继续。
first_syl=input('输入第一个音节:')
第二个音节=输入('输入第二个音节:')
word=输入('输入要翻译的word:')
对于输入字:##对输入字中的所有字符运行循环。
如果ch.lower()不在元音中:##检查ch是否在单词中是元音。
第一个\u元音\u计数=真
元音计数+=1
elif first\u syl和VouelCount中的通配符在您的循环中有一组变量没有被重置:VouelCount
,first\u Vouel\u count
和second\u Vouel\u count
,在您的循环中有一组变量没有被重置:VouelCount
,first\u Vouel\u count
,而second\u元音\u count
如果底部的语句是不必要的,那么如果playreach
是'n'
,就不会在while循环中
无论如何,final\u str
肯定会在那里重置-但是您还需要重置元音计数
,第一个元音计数
,和第二个元音计数
。您可能最好在循环开始时设置它们,使其干燥。将该代码放在中,如果底部的语句是不必要的,那么如果再次播放是'n'
,您就不会在while循环中
无论如何,final\u str
肯定会在那里重置-但是您还需要重置元音计数
,第一个元音计数
,和第二个元音计数
。您最好在循环开始时将其设置为干燥状态。如果再次播放,则有什么意义!=“n”
在这里,您已经在while
条件中检查了这一点。如果再次播放,有什么意义!=“n”
在这里,您已经在while
条件中检查了这一点。
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.