如何获得一个主函数来提示用户输入一个字符串以运行其他操作该字符串的函数?

2024-10-06 07:52:46 发布

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

我正在尝试创建一个程序来使用python的一些字符串操作功能。我做了一些函数,成功地操纵了字符串。我遇到的困难是如何创建一个按顺序运行所有其他函数的主函数。这是我到目前为止的代码

def main():
    print("This program demonstrates Python's String manipulation ability")
    s=input("Enter a String: ")
def change(s):
    firstchar=s[0]
    modifieds=s[1:].replace(firstchar.lower(),"$")
    modifieds=modifieds.replace(firstchar.upper(),"$")
    final=(firstchar+modifieds)
    print(final)
def reverse(s):
res=""
for i in range(len(s)):
    if i%2==0:
        res+=(s[i].lower())
    else:
        res+=(s[i].upper())
return res[::-1]
def code(s):
    string=s.upper()
    n=len(string.split(" "))
    print()
    output=""
    for i in string:
        output+=chr(ord(i)+n)
    print(output)
main()

Tags: 函数字符串outputstringmaindefresupper
2条回答

给你:

def main():
    print("This program demonstrates Python's String manipulation ability")
    return input("Enter a String: ")

def change(s):
    firstchar=s[0]
    modifieds=s[1:].replace(firstchar.lower(),"$")
    modifieds=modifieds.replace(firstchar.upper(),"$")
    final=(firstchar+modifieds)
    print(final)

def rev(s):
    res=""
    for i in range(len(s)):
        if i%2==0:
            res+=(s[i].lower())
        else:
            res+=(s[i].upper())
    return res[::-1]

def code(s):
    string=s.upper()
    n=len(string.split(" "))
    print()
    output=""
    for i in string:
        output+=chr(ord(i)+n)
    print(output)


if __name__ == '__main__':
    user_input = main()
    print(change(user_input))
    print(rev(user_input))
    code(user_input)

对pycodestyle/pep8代码没有影响。我重命名了reverse函数,以免与内置的reverse()方法混淆

我不确定你的目标到底是什么,但这里是它的输出:

james@zingbot:~/Desktop$ ./test.py 
This program demonstrates Python's String manipulation ability
Enter a String: asdf
asdf
None
FdSa

BTEG

如果我没说错的话,你们的反向函数缩进是无效的

相关问题 更多 >