我需要创建一个程序,要求用户输入一个字符串,然后选择以下一个txt编辑器来应用它:
(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()
请帮我修改我的代码。
答案 0 :(得分: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