#πŸ”’ How can I improve my code?

36 messages Β· Page 1 of 1 (latest)

brazen gladeBOT
#

@upbeat oyster

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.

gloomy marsh
#

!paste

brazen gladeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

upbeat oyster
desert swallow
#

looks fine

upbeat adder
gloomy marsh
#

Over here i don't really see any point of this; it just makes the condition statements slightly confusing to read later

    # transforming value saved into variables into numbers for easier usage
    for i in range(len(user_input)):
        if user_input[i] == "Yes" or user_input[i] == "yes":
            user_input[i] = 1
        elif user_input[i] == "No" or user_input[i] == "no":
            user_input[i] = 0

desert swallow
#

you could just make each one of these # doing the same thing for special characters if user_input[1] == 1 and reached_length < pass_length: generated_special_char = random.choice('.:_@#*$%&/£€?!') generated_pass.append(generated_special_char) reached_length += 1 # if user_input[2] == 1 and reached_length < pass_length: generated_number = random.randint(0, 9) generated_pass.append(str(generated_number)) reached_length += 1 # if user_input[3] == 1 and reached_length < pass_length: generated_bracket = random.choice('[({})]') generated_pass.append(generated_bracket) max_brackets += 1 reached_length += 1 to be diffrent functions

brazen gladeBOT
#

Hey @desert swallow!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
gloomy marsh
#

nah, how about we not convert it to anything and just directly compare the string itself

#

saves some complexity and makes it more readable

desert swallow
#

tbh id just unit test it for edge cases then see if it works, cause other than genreal improving of skills, if it works it works

upbeat oyster
#

Well, I thought that converting strings into numbers would've been easier and made more sense

upbeat oyster
desert swallow
#

converting to raw ascii is ok, but with strings you can use regex to evaluate

#

as regex has specific tests for specail chariters/lowercase ect

upbeat adder
upbeat oyster
upbeat oyster
brazen gladeBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
upbeat adder
# brazen glade

@upbeat oyster at the very bottom, this is how you should look up options

gloomy marsh
# gloomy marsh nah, how about we not convert it to anything and just directly compare the strin...

and since you're directly comparing the string itself, you wouldn't need a list. Directly compare the variable itself.

    while reached_length < pass_length:
            generated_letter = random.choice(string.ascii_lowercase)
            generated_pass.append(generated_letter)
            reached_length += 1

            # doing the same thing for special characters
            if include_spec_char == "yes" and reached_length < pass_length:
                generated_special_char = random.choice('.:_@#*$%&/£€?!')
                generated_pass.append(generated_special_char)
                reached_length += 1
            #
            if include_numbers == "yes" and reached_length < pass_length:
                generated_number = random.randint(0, 9)
                generated_pass.append(str(generated_number))
                reached_length += 1
            #
            if include_brackets == "yes" and reached_length < pass_length:
                generated_bracket = random.choice('[({})]')
                generated_pass.append(generated_bracket)
                max_brackets += 1
                reached_length += 1

Looks so much more readable already

upbeat adder
#

!d str.upper - or you can convert it to all caps and compare it that way

brazen gladeBOT
#

str.upper()```
Return a copy of the string with all the cased characters [[4]](https://docs.python.org/3/library/stdtypes.html#id15) converted to uppercase. Note that `s.upper().isupper()` might be `False` if `s` contains uncased characters or if the Unicode category of the resulting character(s) is not β€œLu” (Letter, uppercase), but e.g. β€œLt” (Letter, titlecase).

The uppercasing algorithm used is [described in section 3.13 β€˜Default Case Folding’ of the Unicode Standard](https://www.unicode.org/versions/Unicode15.1.0/ch03.pdf).
gloomy marsh
upbeat oyster
#

Yeah I understand, I'll check your solution and see what I can do about it

#

Thankd for the help everyone πŸ™‚

gloomy marsh
#

meanwhile later you could also learn type hinting

#

pretty useful tool for development

upbeat oyster
#

Type hinting, okay I'll check it out

gloomy marsh
#

doesnt make your code faster or anything, but it provides your editor to predict the types of various variables which makes development much faster when you're in doubt what type aa variable holds

upbeat oyster
#

Oooh okay, that's great! thanks!

brazen gladeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.