I mean, this actually isn’t that bad. One of the mottos of python is “it’s better to ask for forgiveness than for permission”, as in its easier to just try something and then if it results in an error, just catch the exception and move on.
Now, that’s assuming line 27 causes error on non strings, which is a false assumption. Python will happily add anything to a string with a to string method.
I don’t think try/except is the big issue here, rather than this being the same as ’’.join(array) which could also be put in a try block to ensure it can be joined. Oh and also, you shouldn’t exceptall exceptions but make those explicit.
# adds all strings in array together
def add_together(array):
output = ''
for string in array:
try:
output = output + string
except:
print('element in array is not string')
return output
2
u/jaysuchak33 ifelseifelseifelseifelseifelseifelseifelseifelseifelseifelse Sep 07 '21
what extension is this?
41
u/dyingpie1 Sep 07 '21
I mean, this actually isn’t that bad. One of the mottos of python is “it’s better to ask for forgiveness than for permission”, as in its easier to just try something and then if it results in an error, just catch the exception and move on.
Now, that’s assuming line 27 causes error on non strings, which is a false assumption. Python will happily add anything to a string with a to string method.
20
u/RedditAcc-92975 Sep 07 '21
sry, no upvote, not bad enough
13
u/seeroflights Sep 07 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!