【问题标题】:String Won't Reset Correctly at the End of Loop - Python字符串在循环结束时不会正确重置 - Python
【发布时间】:2012-09-16 12:52:38
【问题描述】:

我创建了一个程序,您可以在其中输入一个单词,并在输入的单词的每个元音之前添加某些文本。程序将要求用户再次播放,直到输入“n”表示否。

但是,当输入“y”时,它会继续播放,但是会发生这样的情况:

第一次运行程序:

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)? 

第一个结果“gipibbezzerizzish”是正确的。第二次运行时出现错误,结果“gizzibbezzerizzish”不正确。

在循环结束时我所做的是 make final_str = "" 所以当它开始备份时,它是空的,但由于某种原因这不起作用?这里有什么问题?

我正在使用 Python 3.2.3。

代码:

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.

【问题讨论】:

  • 这里if playagain != "n" 的意义何在,您已经在while 条件中检查了这一点。

标签: python string loops


【解决方案1】:

您的循环中有许多变量没有被重置:vowelcountfirst_vowel_countsecond_vowel_count

【讨论】:

    【解决方案2】:

    没有必要在底部的if 语句中包含该代码,如果playagain'n',您将不会进入while 循环。

    无论如何,final_str 肯定会在那里重置 - 但您还需要重置 vowelcountfirst_vowel_countsecond_vowel_count。你最好只在循环的开头设置它们以便干燥。

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 1970-01-01
      • 2015-08-23
      • 2018-06-25
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多