Shorting Code down - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Shorting Code down (/thread-426.html) Pages:
1
2
|
Shorting Code down - gh01 - Oct-10-2016 Hi I've made some code but it seems a bit long is their a way to shorten it down because it seems too long . This is the code: if password.isalnum(): flag=flag+2 if not password.isupper(): if not password.islower(): flag=flag+1 elif not password.islower(): if not password.isupper(): flag=flag+1 elif password.isnumeric(): flag=flag+1 elif password.isalpha(): if password.isupper(): flag=flag+1 elif not password.islower(): flag=flag+1 if not password.isupper(): if not password.islower(): flag=flag+1 elif not password.islower(): if not password.isupper(): flag=flag+1 flag=flag-1 if not password.isalpha(): if not password.isnumeric(): flag=flag+1 if password.isnumeric(): flag=flag-1 if flag==3:if there is a way to shorten it down i would be grateful but if you need the entire thing it is below Passwordlist = [] Usernamelist = [] flag=0 create=True UP= input("do you have a username and password?") if UP == "yes" : Username = input("Enter your username") Password = input("Enter you password") elif UP == "no": while (create) == True : create = input("Do you want to create a user name and password?") if create == "yes": username = input("Create your username") Usernamelist.append(username) password= input("Create your password") Passwordlist.append(password) elif create == "no": if len (password) <6: str(print('Change password to be 6 characters or more than 6 characters')) elif len(password) >12: print('Change password to be 12 characters or less') else : print(' You have the correct amount of characters') print('Checking the strength for your password') if password.isalnum(): flag=flag+2 if not password.isupper(): if not password.islower(): flag=flag+1 elif not password.islower(): if not password.isupper(): flag=flag+1 elif password.isnumeric(): flag=flag+1 elif password.isalpha(): if password.isupper(): flag=flag+1 elif not password.islower(): flag=flag+1 if not password.isupper(): if not password.islower(): flag=flag+1 elif not password.islower(): if not password.isupper(): flag=flag+1 flag=flag-1 if not password.isalpha(): if not password.isnumeric(): flag=flag+1 if password.isnumeric(): flag=flag-1 if flag==3: print( ' Your password is STRONG') Passwordlist.append(password) print(Usernamelist) print(Passwordlist) e=str(input(('Press [E] t exit'))).upper() if e=='E': break if flag==2: print ('Your password is MEDIUM') Passwordlist.append(password) print(Usernamelist) print(Passwordlist) e=str(input(('Press [E] to exit'))).upper() if e=='E': break if flag==1: print ('Your password is WEAK ') Passwordlist.append(password) print(Usernamelist) print(Passwordlist) e=str(input(('Press [E] to exit'))).upper() if e=='E': RE: Shorting Code down - Mekire - Oct-10-2016 You need to be more clear about the requirements of your passwords and what makes a weak, medium, or strong password in this case. Your current code looks amazingly inefficient. RE: Shorting Code down - micseydel - Oct-10-2016 (Oct-10-2016, 06:21 PM)Mekire Wrote: Your current code looks amazingly inefficient.Do you mean just by LOC? +1 to clarified requirements. Reverse engineering the code is possible but unnecessarily time consuming. RE: Shorting Code down - gh01 - Oct-10-2016 so basically what the main objective is that passwords are sorted into a system of weak is 1 flag and 1 flag is represented by either a uppercase letter, lowercase letter or a number but neither of them can be together medium is represented by 2 flags and that is either a uppercase letter, lowercase letter or a number but 2 of them are together it doesn't matter which one and strong is 3 flags which is either a uppercase letter, lowercase letter or a number but altogether. RE: Shorting Code down - micseydel - Oct-10-2016 That's still not clear. Can you provide a table or other well-organized documentation? And some examples? RE: Shorting Code down - Mekire - Oct-10-2016 (Oct-10-2016, 06:29 PM)micseydel Wrote:(Oct-10-2016, 06:21 PM)Mekire Wrote: Your current code looks amazingly inefficient.Do you mean just by LOC? Yeah, in this case I mean inefficiently written. Not literal (time/space) complexity. RE: Shorting Code down - gh01 - Oct-10-2016 (Oct-10-2016, 06:34 PM)micseydel Wrote: That's still not clear. Can you provide a table or other well-organized documentation? And some examples? i dont have anything else RE: Shorting Code down - Mekire - Oct-10-2016 I believe this captures your idea as far as I understand it: password = input("Enter password: ") min_size = 6 strength = ["Too short", "Weak", "Medium", "Strong"] security = 0 if len(password) >= min_size: if any(char.isdigit() for char in password): security += 1 if any(char.isupper() for char in password): security += 1 if any(char.islower() for char in password): security += 1 print(strength[security])Seem correct? RE: Shorting Code down - micseydel - Oct-10-2016 (Oct-10-2016, 06:50 PM)gh01 Wrote: i dont have anything elseNot even examples? RE: Shorting Code down - gh01 - Oct-10-2016 yes something like that but i also need it so it can have a character counting system intergrated into it and it can have a password up to 6-12 characters or is my system good enough |