2016-02-25 200 views
1

一個文本編輯器,我需要建立一個程序,要求用戶輸入一個字符串,然後選擇以下TXT編輯之一,以適用於它:實現在Python

(lcase) make each letter lowercase 
(ucase) make each letter uppercase 
(alt) make each letter alternate in case, with first letter capitalized 
(remspace) remove all white space 
(1337) convert to l337-speak 
(rev) reverse the string 
(new) enter a new string 
(restore) replace string with last one entered 
(quit) exit the program 

程序需要運行一個循環,直到用戶輸入退出。

這是到目前爲止我的代碼,但它並沒有真正的工作(提示工作完美,但我不能得到的改性劑在所有的工作):

print "Welcome to the text converter. Your options are to:" 
print "(lcase) make each letter lowercase" 
print "(ucase) make each letter uppercase" 
print "(alt) make each letter alternate in case, with first letter capitalized" 
print "(remspace) remove all white space" 
print "(1337) convert to l337-speak" 
print "(rev) reverse the string" 
print "(new) enter a new string" 
print "(restore) replace string with last one entered" 
print "(quit) exit the program" 
print "-----------------------------------------------" 
y = raw_input("Please enter a string: ") 

def reverse_string(y): 
    return y[:: -1] 

x = raw_input("Action (lcase, ucase, alt, remspace, rev, new, restore, quit):") 


while x == raw_input: 
    if x == "lcase": 
     return str.lower(y) 

    if x == "ucase": 
     return y.upper(y) 

    if x == "alt": 
     return 

    if x == "remspace": 
     return str.string(y) 

    if x == "rev": 
     return 

    if x == "restore": 
     return y 

    if x == "quit": 
     print "See you next time!" 
     exit() 

    if x == "new": 
     break 
     return raw_input() 

請幫我解決我的代碼。

回答

2

while x == raw_input將永遠達不到。
return應該在一個函數內。

print "Welcome to the text converter. Your options are to:" 
print "(lcase) make each letter lowercase" 
print "(ucase) make each letter uppercase" 
print "(alt) make each letter alternate in case, with first letter capitalized" 
print "(remspace) remove all white space" 
print "(1337) convert to l337-speak" 
print "(rev) reverse the string" 
print "(new) enter a new string" 
print "(restore) replace string with last one entered" 
print "(quit) exit the program" 
print "-----------------------------------------------" 

def reverse_string(y): 
    return y[:: -1] 

while True: 
    y = raw_input("Please enter a string: ") 
    x = raw_input("Action (lcase, ucase, alt, remspace, rev, new, restore, quit):") 

    if x == "lcase": 
     print y.lower() 

    if x == "ucase": 
     print y.upper() 

    if x == "alt": 
     #Capitalize the first letter of each word in a string 
     print y.title() 

    if x == "remspace": 
     # Replace each 'space' with '' 
     print y.replace(' ', '') 

    if x == "rev": 
     #call 'reverse_string' to reverse 'y' 
     print reverse_string(y) 

    if x == "restore": 
     print y 

    if x == "quit": 
     print "See you next time!" 
     #breaks while loop 
     break 

    if x == "new": 
     #Go to the next iteration (Ask user again) 
     continue 
+0

@Alex Loftus請隨時接受本答覆。 – Zety