在python

时间:2017-06-10 21:16:40

标签: python

我刚开始学习python,我已经遇到了障碍。

我正在尝试为Pig Latin转换器编写代码,我正在使用函数来编写它。我在while循环中运行。

我试图弄清楚如何通过使用break来结束循环,但它不会结束,只有在第二次输入时才有效。

我想知道我做错了什么,如果你们能帮助我,我会非常感激。谢谢:))

    pig = 'ay'

    def func(): 
        word = input('Hello. Please Enter your First Word:').lower()


        if len(word) > 0 and word.isalpha():
            first = word[0]
            if first in ('a', 'e', 'i', 'o', 'u'):
                print('The first letter must begin with a consonant. Would you like to try a different word? (Y/N)') and inpup().lower()
                if input() == 'y':
                    return func()
                else:
                    print("Thank you for using Pig Latin Converter")
                    return False



        else:
            new_word = word[1].upper() + word[2:] + ' ' + first.upper() + pig
            print(new_word)
            print("Would you like to try a different word? (Y/N)") and input().lower()
            if input() == 'y':
                return func()
            else:
                print("Thank you for using Pig Latin Converter")
                return False



while True:
    func()

    if False:
        break

2 个答案:

答案 0 :(得分:1)

while True:
    func()

    if False:
        break

func()返回值True,但未分配给任何内容。 while语句仍然是True,并继续循环。把它改成这样的东西

proceed = True
while proceed:
    proceed = func() 

这次循环第一次运行,因为proceed为true,执行func()然后取决于func()的返回值 - 无论是True还是False,正文将重复或完成。请注意,如果代码执行为True,则需要更改retrun值,例如..

if input() == 'y':
    return True

答案 1 :(得分:-1)

首先,这是您的功能的清洁版本。您可以通过调用它来运行转换器,不需要while循环,因为您的函数已经递归。

def func(): 
    pig = 'ay' # Moved this inside as there was no need for it to be defined out of the function's scope.
    word = input('Hello. Please Enter a Word:').lower() # Changed this so it doesn't say 'First' everytime


    if not (len(word) > 0 and word.isalpha()):
        # If you end up in here then the user entered something incorrectly, So we should tell them that. 
        print('The word needs to contain alphabetic characters only. Would you like to try again? (Y/N)') and inpup().lower()
        if input() == 'y':
            # Note that your function is recursive, so you don't need to have a while loop.  
            return func()
        else:
            print("Thank you for using Pig Latin Converter")
            return False
    first = word[0]
    if first in ('a', 'e', 'i', 'o', 'u'):
        print('The first letter must begin with a consonant. Would you like to try a different word? (Y/N)') and inpup().lower()
        if input() == 'y':
            return func()
        else:
            print("Thank you for using Pig Latin Converter")
            return False



    else:
        # I had to replace the 'first' variable with 'word[0]'. This is because if the user entered a valid string then you would never assign the first variable.
        new_word = word[1].upper() + word[2:] + ' ' + word[0].upper() + pig
        print(new_word)
        print("Would you like to try a different word? (Y/N)") and input().lower()
        if input() == 'y':
            return func()
        else:
            print("Thank you for using Pig Latin Converter")
            return False
func()

如果你运行它应该工作:-)我已经在任何地方发表评论我改变了一些东西。