我想知道如何在python中重复一个过程?

2024-04-20 06:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

print "You've entered a wording system!"
print "What words do you want?"

while False:
    word1 = raw_input('enter your word:')
    word2 = raw_input('enter your word:')

print word1
print word2

if len(word1)>len(word2):
    word_difference = len(word1) - len(word2)
    print word1, "is", word_difference, "letters longer than", word2

elif len(word2)>len(word1):
    word_difference = len(word2) - len(word1)
    print word2, "is", word_difference, "letters longer than", word1

elif len(word1) == len(word2):
    print word1, "has same number of letters as", word2    

repeat = raw_input("Would you like to enter two more words?(y/n)")    

if repeat == "y":
    y == False

所以我想创建一个从word1 = raw input到{}的代码,如果问题要求你"Would you like to enter two more words" is == y,如果no == Good bye!


Tags: youfalseinputyourrawlenisword
2条回答

只是迭代器来迭代和重复这个过程。有关详细信息,请参阅参考链接http://www.pythonlearn.com/html-008/cfbook006.html

只需将all代码放入循环中,只要repeat(初始化为“y”保持等于“y”)就迭代:

print "You've entered a wording system!"
print "What words do you want?"

repeat = "y"
while repeat == "y":
    word1 = raw_input('enter your word:')
    word2 = raw_input('enter your word:')

    print word1
    print word2

    if len(word1)>len(word2):
        word_difference = len(word1) - len(word2)
        print word1, "is", word_difference, "letters longer than", word2

    elif len(word2)>len(word1):
        word_difference = len(word2) - len(word1)
        print word2, "is", word_difference, "letters longer than", word1

    elif len(word1) == len(word2):
        print word1, "has same number of letters as", word2    

    repeat = raw_input("Would you like to enter two more words?(y/n)")

相关问题 更多 >