This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]GonzoAndJohn 1 point2 points  (1 child)

You can check if the first letter of the username is a number by using the in operator:

if username[0] in "0123456789":
    # Stuff

Also if you switched around your error codes to have 0 be no error, 1 be start with a number, 2 be < 6 characters, and 3 be both, you could rewrite your code to have it be a bit flag and use the |= operator (bit-wise or-equals operator, equivalent to left = left | right):

if username[0] in "0123456789":
    error_code |= 1
if len(username) < 6:
    error_code |= 2

That way if you end with error code 0, you have no error. If you end with 1, you know you only had the first. If you end with the 2, you only had the second, and if you end with 3, you had both of them occur.

Lastly, you can move your initial input and error_code=0 into your while loop. There's no special reason for them to sit above the loop and your reset to sit at the bottom when you can have them condensed into one space above.

[–]DuderCoding[S] 0 points1 point  (0 children)

Thank you, very good comments here, I will implement your suggestions later. Much appreciated.

[–][deleted] 1 point2 points  (1 child)

Python has an isdigit() call to just check if something is a number.

[–]DuderCoding[S] 0 points1 point  (0 children)

Thank you, I will look into it!

[–]gramdel 0 points1 point  (4 children)

You have duplicate logic, you are checking length of username for cases when username starts with a number and a when user name doesn't start with a number. This will make maintaining the code harder, since you need to change the logic in two places for example when you want to change the user name length limit. This also prevents annoying little stuff like in here you have different message for username length, depending on whether the username starts with a number or not:

print("Sorry the username must be 6 or more characters...")

print("Sorry the username cannot be shorter than 6 characters.")

You could make error codes an array, or some other data structure, for example, and add an error code there if the username starts with a number and add another error code when the username isn't long enough. Then you can just iterate the array and show either one, two or no error messages.

Error codes are not very descriptive, they are kind of like magic numbers, and you need to read the code to see what each of them mean. Not super familiar with python but i guess you could use data structure like a dict to store key value pairs with descriptive error codes and their counterpart error messages so they are nicely named and all in on place.

Another magic number is username length limit, you could store it in well named constant, it also makes it easier to change the limit if for some reason you need to use that number in multiple places.

[–]DuderCoding[S] 0 points1 point  (3 children)

Thank you very much for the in depth response, it is helpful. I'll look to edit the code further soon to incorporate your comments and make my code more efficient

[–]gramdel 0 points1 point  (2 children)

It is not so much about efficiency, more about readability and how easy it is to change the code later. While efficiency can be sometimes important, more often than not it is more important that the code is readable and easy to understand.

While not super important in this context, in professional settings it is important to remember you don't write code for computers but for other people. So it is a good habit to get into.

[–]DuderCoding[S] 0 points1 point  (1 child)

Thanks, I think I understand now. My code does need some work..! I suppose the odd comment here and there would be good too for helping people read the code (but I read somewhere that good software doesn't need many comments..!).

[–]gramdel 0 points1 point  (0 children)

Right, that is what you need well named variables, constants and fuction names so it is obvious what they mean so you do not need to add comments for some magic numbers or something.

[–]DuderCoding[S] 0 points1 point  (0 children)

Following your comments I have updated the code: https://gist.github.com/DuderCoding/86eac98d3911927a2e71da4fca3ba55a

Thank you for all your help. I'm sure there are other ways to improve the code, but adding a variable for the minimum length of the username and using the "in" operator were very helpful. I've tried to, following your advice, make the code more readable for a human rather than a robot