【发布时间】: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
capitalto be callingmain, rather thanmainbeing solely the main entry point for your program. -
If
capitalis really the entry point and it callsmain()to get the initial input, thenitshould be printing the value ofspacebefore it returns the capitalized version, rather than you callingmainexplicitly to getanotherstring.
标签: python