Best Python code snippet using tox_python
main.py
Source:main.py
...27 else:28 return False293031def ask_user(text, text_about_the_warning, func, type=float):32 while True:33 try:34 n = input(text)35 if not func(n):36 raise ValueError37 break38 except ValueError:39 print(text_about_the_warning)40 return type(n)414243def main():44 while True:45 choice = input("ÐведÑÑÑ ÑÑнкÑÑÑ, ÑÐºÑ Ð²Ð¸ Ñ
оÑеÑе викоÑиÑÑовÑваÑи: ")46 try:47 if choice == 'fact':48 n = ask_user("ÐведÑÑÑ ÑиÑло: ", "Ðи Ð¿Ð¾Ð²Ð¸Ð½Ð½Ñ Ð²Ð²ÐµÑÑи ÑÑле ÑиÑло!!!", lambda x: int(x) >= 0, int)49 print(f"ФакÑоÑÑал ÑиÑла {n} --- {fact(n)}")50 elif choice == 'exp2':51 n = ask_user("ÐведÑÑÑ ÑиÑло: ", "Ðи маÑÑе ввеÑÑи ÑиÑло!!!", lambda x: float(x))52 print(f"ÐвадÑÐ°Ñ ÑиÑла {n} --- {exp2(n)}")53 elif choice == 'exp3':54 n = ask_user("ÐведÑÑÑ ÑиÑло: ", "Ðи маÑÑе ввеÑÑи ÑиÑло!!!", lambda x: float(x))55 print(f"ÐÑб ÑиÑла {n} --- {exp3(n)}")56 elif choice == 'root2':57 n = ask_user("ÐведÑÑÑ ÑиÑло: ", "Ðи Ð¿Ð¾Ð²Ð¸Ð½Ð½Ñ Ð²Ð²ÐµÑÑи ÑиÑло, Ñо >= 0.", lambda x: float(x) >= 0)58 print(f"ÐвадÑаÑний коÑÑÐ½Ñ ÑиÑла {n} --- {root2(n)}")59 elif choice == 'root3':60 n = ask_user("ÐведÑÑÑ ÑиÑло: ", "Ðи маÑÑе ввеÑÑи ÑиÑло!!!", lambda x: float(x))61 print(f"ÐÑбÑÑний коÑÑÐ½Ñ ÑиÑла {n} --- {root3(n)}")62 elif choice == 'log':63 a = ask_user("ÐведÑÑÑ Ð¾ÑÐ½Ð¾Ð²Ñ Ð»Ð¾Ð³Ð°ÑиÑма: ", "Ðи Ð¿Ð¾Ð²Ð¸Ð½Ð½Ñ Ð²Ð²ÐµÑÑи додаÑне ÑиÑло, вÑдмÑнне вÑд 1!!!",64 lambda x: float(x) > 0 and float(x) != 1)65 b = ask_user("ÐведÑÑÑ ÑиÑло: ", "ÐоÑÑÑбно ввеÑÑи додаÑне ÑиÑло!!!.", lambda x: float(x) > 0)66 print(f"ÐогаÑиÑм ÑиÑла {b} по оÑÐ½Ð¾Ð²Ñ {a} --- {log(a, b)}")67 elif choice == 'ln':68 b = ask_user("ÐведÑÑÑ ÑиÑло: ", "ÐоÑÑÑбно ввеÑÑи додаÑне ÑиÑло!!!", lambda x: float(x) > 0)69 print(f"ÐаÑÑÑалÑний логаÑиÑм ÑиÑла {b} --- {ln(b)}")70 elif choice == 'lg':71 b = ask_user("ÐведÑÑÑ ÑиÑло: ", "ÐоÑÑÑбно ввеÑÑи додаÑне ÑиÑло!!!", lambda x: float(x) > 0)72 print(f"ÐеÑÑÑковий логаÑиÑм ÑиÑла {b} --- {lg(b)}")73 else:74 raise ValueError75 except ValueError:76 print("Ðам необÑ
Ñдно ввеÑÑи Ð¾Ð´Ð½Ñ Ñз запÑопонованиÑ
ÑÑнкÑÑй!!!")77 else:78 result = finish()79 if result:80 break818283if __name__ == '__main__':
...
PWgenDelich.py
Source:PWgenDelich.py
...13number = True14spec = True1516#method for checking user input17def ask_user():18 global lower, upper, number, spec, length, amount, check19 check = input("What length would you like your password to be? ")20 try:21 if check.strip().isdigit():22 length = check23 length = int(length)24 else:25 print("invalid input. input must be number")26 return ask_user()27 except:28 print("Please enter valid inputs")29 return ask_user()30 check = input("How many passwords would you like to generate? ")31 try:32 if check.strip().isdigit():33 amount = check34 amount = int(amount)35 else:36 print("invalid input. input must be number")37 return ask_user()38 except:39 print("Please enter valid inputs")40 return ask_user()41 check = str(input("Do you want to include lowercase characters? (Y/N)")).lower().strip()42 try:43 if check[0] == "y":44 lower = True45 elif check[0] == "n":46 lower = False47 else: 48 print("invalid input")49 return ask_user()50 except:51 print("Please enter valid inputs")52 return ask_user()53 check = str(input("Do you want to include uppercase characters? (Y/N)")).lower().strip()54 try:55 if check[0] == "y":56 upper = True57 elif check[0] == "n":58 upper = False59 else: 60 print("invalid input")61 return ask_user()62 except:63 print("Please enter valid inputs")64 return ask_user()65 check = str(input("Do you want to include number characters? (Y/N)")).lower().strip()66 try:67 if check[0] == "y":68 number = True69 elif check[0] == "n":70 number = False71 else: 72 print("invalid input")73 return ask_user()74 except:75 print("Please enter valid inputs")76 return ask_user()77 check = str(input("Do you want to include special characters? (Y/N)")).lower().strip()78 try:79 if check[0] == "y":80 spec = True81 elif check[0] == "n":82 spec = False83 else: 84 print("invalid input")85 return ask_user()86 except:87 print("Please enter valid inputs")88 return ask_user()8990#executing the function to get info91ask_user()9293#cheking which combination is selected94if lower:95 combine += lowercase96if upper:97 combine += uppercase98if number:99 combine += nums100if spec:101 combine += special102103for x in range(amount):104 temp = random.sample(combine, length)105 password = "".join(temp)
...
ask_user.py
Source:ask_user.py
1import getpass2import sys3def ask_user(user_prompt, user_options=None, password=False, catch=(KeyboardInterrupt, EOFError)):4 '''5 ask_user('prompt')6 ask_user('prompt', ('y', 'n'))7 ask_user('prompt', 'yn')8 Returns:9 user input10 None, when stdin is not a tty (e.g. a pipe)11 EOFError12 KeyboardInterrupt13 '''14 if not sys.stdin.isatty():15 return None16 elif user_options is None:17 options = []18 prompt = user_prompt + ('' if user_prompt.endswith(' ') else '> ')19 elif isinstance(user_options, (list, tuple, str)):20 if ' ' in user_options:21 raise ValueError('Cannot have space in user_options')22 options = (user_options[0].capitalize(),) + tuple(user_options[1:])23 prompt = '{prompt} [{options}] '.format(24 prompt=user_prompt.rstrip(),25 options='/'.join(options),26 )27 else:28 raise ValueError('Unrecognized user_options: {}'.format(user_options))29 try:30 if password:31 ret = getpass.getpass(prompt)32 else:33 ret = input(prompt).strip().lower()34 if options and not ret:35 ret = user_options[0]36 except catch as e:37 ret = type(e)38 return ret39if __name__ == '__main__':40 print(ask_user('Password> ', password=True))41 print(ask_user('prompt', catch=(KeyboardInterrupt,)))42 print(ask_user('prompt', 'yn', catch=(EOFError,)))43 print(ask_user('prompt', ('y', 'n')))44 print(ask_user('prompt', ('yes', 'no')))45 print(ask_user('prompt', ('apple', 'banana')))46 print(ask_user('prompt', 5))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!