2014-09-26 52 views
0
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,退出,即)在python中循環這個程序的最佳方式是什麼?

任何提示,讚賞。謝謝。

+2

你已經使用了'str.lower',那麼輸入是用低位還是大寫字母還是很重要? – starrify 2014-09-26 03:45:53

回答

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

new_word = new_word [1:]

##是更好! q若要在NOM分割的第一個字

NOM = 'AlassaneAli'

MYLIST =列表()

對於i:

mylist.append(i) 

用於在範圍(LEN(MYLIST) ):

print(mylist[a]) 

打印(MYLIST [1:])

+0

根本不回答原始問題。你確定你打算回答這個問題,而不是其他人嗎? – rmunn 2014-09-26 05:28:39

相關問題