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

时间:2014-09-10 12:56:08

标签: python python-2.7

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以及repeat = raw_input(asking fort two more words)

,我想创建一个从"Would you like to enter two more words" is == y重复到no == Good bye!的代码

2 个答案:

答案 0 :(得分:0)

只需将所有代码放入循环中,只要repeat进行迭代(初始化为" y"保持等于" y&#34 ):

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

答案 1 :(得分:0)

Just Iterator迭代并重复该过程。有关详细信息,请参阅参考链接http://www.pythonlearn.com/html-008/cfbook006.html