在python中循环这个程序的最佳方法是什么?

时间:2014-09-26 03:41:10

标签: python loops

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    print original    
    word = original.lower()
    first = word[0]
    second = word[1]
    new_word = word + first + pyg  
    new_word = new_word[1:len(new_word)]
    print new_word
else:
    print 'empty'

我需要这个循环,并且当用户使用低位或大写字母的任意组合输入“退出”时。 (qUit,QUIT,quIT,即。)

任何提示都表示赞赏。感谢。

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

pyg = 'ay'
original = raw_input('Enter a word:')

while original and original.strip().upper() != 'QUIT':
    # loop body
    if len(original) > 0 and original.isalpha():
        print original    
        word = original.lower()
        first = word[0]
        second = word[1]
        new_word = word + first + pyg  
        new_word = new_word[1:len(new_word)]
        print new_word
    else:
        print 'empty'
    # read again, for next loop
    original = raw_input('Enter a word:')

答案 1 :(得分:0)

试试这个

pyg = 'ay'

while True:

    original = raw_input('Enter a word:')

    if original.upper() == 'QUIT':
        break

    if len(original) > 0 and original.isalpha():
        print original    
        word = original.lower()
        first = word[0]
        second = word[1]
        new_word = word + first + pyg  
        new_word = new_word[1:len(new_word)]
        print new_word
    else:
        print 'empty'
        break

答案 2 :(得分:-1)

new_word = new_word [1:]

##它更好! #to拆分第一个单词

nom ='AlassaneAli'

mylist = list()

代表我的名字:

    mylist.append(i)

对于范围内(len(mylist)):

print(mylist[a]) 

print(mylist [1:])