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()
請幫我解決我的代碼。
@Alex Loftus請隨時接受本答覆。 – Zety