#πŸ”’ I want to know if my code is efficient or needlessly complicated

36 messages Β· Page 1 of 1 (latest)

eager ravine
#

Basically, I was doing an exercise. The code I ended up writing for it is different than the official answers for that exercise. And I wonder whether that's a bad thing or not.

vagrant heraldBOT
vagrant heraldBOT
#

@eager ravine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grave hawk
#

I'd likely use a loop

eager ravine
grave hawk
#

it's good though that you already see that it's best not to repeat yourself in code

eager ravine
#

:D

rough olive
#

if it's me I would keep message as is and do .upper() when printing

#

secondly to that I'd use uppermessage like your first one because it's clearer whats going on

eager ravine
#

?

#

that makes sense

#

like you mean you would do this one?

grave hawk
#

you don't even really need a new variable

#

print(message.upper(), '!!!')

#

!e

message = 'Hello'

for i in range(3):
    print(f'{message.upper()}!!!')

vagrant heraldBOT
eager ravine
grave hawk
#

it's one example, yes

eager ravine
#

that looks really cool, both readability and efficiency

eager ravine
grave hawk
#

python has for and while keywords for creating loops

eager ravine
#

hmmm

#

i see

#

i will learn more about loops in the future im using this one github tutorial but i havent gotten to that point yet

grave hawk
#

range makes it very easy to make something loop "a certain number of times"

eager ravine
#

but this is interesting and helpful

grave hawk
#

you can try changing the number inside range(3) to see the result

eager ravine
grave hawk
#

exactly πŸ™‚

eager ravine
#

gotcha, thank you for all the insight Miku_love

grave hawk
#

np!

eager ravine
#

!close

vagrant heraldBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.