我想知道如何在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重复到repeat = raw_input(asking fort two more words)一个代码,如果no == Good bye!我想知道如何在Python中重复一个过程?

+1

你已经告诉你如何编写一个while循环...为什么不使用另一个? – James 2014-09-10 13:00:28

+3

'while False'永远不会被执行,因为它是,呃,不是True。 – cdarke 2014-09-10 13:00:32

+0

它看起来像所有的代码应该在第一个循环中,只要'repeat'等于“y”(在循环之前已经适当地初始化了'repeat'),它应该执行。 – chepner 2014-09-10 13:03:46

只要把所有的代码在你的循环,其迭代只要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)") 

只是迭代器来迭代和重复该过程。欲了解更多信息,请参阅参考链接http://www.pythonlearn.com/html-008/cfbook006.html