【问题标题】:passing input between multiple functions?passing input between multiple functions?
【发布时间】:2022-11-20 10:04:37
【问题描述】:

im currently trying to pass input between multiple functions. As of now im having an extremely hard time figuring out how to do it with my program. My program consists of 2 functions. main() will get the user input, remove all punctuation and capital() will take that output and turn it into all caps. However, when i call the function it only prints it fully capitalized rather than printing it first without the punctuation and then fully capitalized.

here is what ive tried. I set the space variable = to my main function so i can pass on the string thats produced from main. However im getting the error from above and feel my solution is extremely inefficient. if anyone has a way to do this without using a global constant or global variable please let me know. the was im trying to do this is with parameters but i am very confused as to why this is happening. thanks

punctuation = "!@#$%^&*():<>?{}[]`\/~"


def capital():
    space = main()
    string2 = ''
    for i in range(len(space)):
        if(space[i] >= 'a' and space[i] <= 'z'):
            string2 = string2 + chr((ord(space[i]) - 32))
        else:
            string2 = string2 + space[i]
    return string2



def main():
    user_string=input("Please enter a string: ")
    space  = ""
    for character in user_string:
        if character not in punctuation:
            space = space+character
    return space

print(capital())
print(main())
```
`

【问题讨论】:

  • Maybe it's just a naming issue, but it seems odd for capital to be calling main, rather than main being solely the main entry point for your program.
  • If capital is really the entry point and it calls main() to get the initial input, thenitshould be printing the value of space before it returns the capitalized version, rather than you calling main explicitly to getanotherstring.

标签: python


【解决方案1】:

The reason this might be happening is because you are calling main() prior to assignment, which does not work on some versions of python if I remember correctly. You could update to a newer version, but a better way is to use parameters like you explained.

To make a parameter, you could have your capital() function take in a variable. To do this, simply write the name of the local variable (only usable inside the function) inside the parentheses: def capital(space): Then, all you need to do is run capital() and pass in main() as space:

def capital(space):
    string2 = ''
    for i in range(len(space)):
        if(space[i] >= 'a' and space[i] <= 'z'):
            string2 = string2 + chr((ord(space[i]) - 32))
        else:
            string2 = string2 + space[i]
    return string2



def main():
    user_string=input("Please enter a string: ")
    space  = ""
    for character in user_string:
        if character not in punctuation:
            space = space+character
    return space

print(capital(main()))

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 2022-12-19
    • 2022-12-02
    • 1970-01-01
    • 2011-11-12
    • 2020-06-04
    • 2022-12-01
    • 2015-10-10
    • 2012-06-25
    相关资源
    最近更新 更多