简体   繁体   中英

What's the best way to loop this program in python?

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'

I need this to loop and and when the user inputs 'Quit' with any combination of lower or capital letters. (qUit, QUIT, quIT, ie)

any tips are appreciated. Thanks.

Try something like this:

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:')

try this

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

new_word = new_word[1:]

## it's better! #to split the first word

nom = 'AlassaneAli'

mylist = list()

for i in nom:

  mylist.append(i) 

for a in range(len(mylist)):

 print(mylist[a]) 

print(mylist[1:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM