#π How can I improve my code?
36 messages Β· Page 1 of 1 (latest)
@upbeat oyster
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.
!paste
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.
Sorry I'm new here I've pasted the code on the link: https://paste.pythondiscord.com/SH7A π
looks fine
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
why not just directly turn the inputs into booleans?
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
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
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.
that's what i said 
nah, how about we not convert it to anything and just directly compare the string itself
saves some complexity and makes it more readable
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
Well, I thought that converting strings into numbers would've been easier and made more sense
So you mean turning them into True/False ?
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
yeah i'd say so
Oh the ascii part I got it from w3school because I didn't know how to do differently
Okay then! Thanks
!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 oyster at the very bottom, this is how you should look up options
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
!d str.upper - or you can convert it to all caps and compare it that way
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).
it makes it slower and harder to read so idk what exactly makes it easier
Yeah I understand, I'll check your solution and see what I can do about it
Thankd for the help everyone π
meanwhile later you could also learn type hinting
pretty useful tool for development
Type hinting, okay I'll check it out
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
Oooh okay, that's great! thanks!
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.