在Python中实现文本编辑器

2024-09-29 19:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个程序,要求用户输入一个字符串,然后选择以下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

程序需要在循环中运行,直到用户输入quit。你知道吗

到目前为止,这是我的代码,但它并没有真正起作用(提示工作得很好,但我无法让修饰符工作):

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()

请帮我修改代码。你知道吗


Tags: thenewstringmakereturnifwithalt
1条回答
网友
1楼 · 发布于 2024-09-29 19:20:29

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

相关问题 更多 >

    热门问题