Best Python code snippet using localstack_python
Coffee.py
Source:Coffee.py
1#éå¯åçåå§æ°æ®2contains_list ={"Water":100,3 "Milk":100,4 "Coffe": 100,5 "Mocha": 100}6kinds_list = {"Cappuccino": 0,7 "Latte": 0,8 "Mocha": 0,9 "CafeAmericano": 0,10 "Espresso" : 0}11#å½æ°å°è£
ï¼1.æ¹ä¾¿å¾ªç¯ã2.æ¹ä¾¿åé循ç¯çè·³åºã12def coffee():13 menu_list =[14 {"id": 1, "name":"1.Cappuccino","contains":"[Water: 50ml; Milk: 50ml; Coffee: 50ml]:","price":12,"$":"ï¿¥"},15 {"id": 2, "name":"2.Latte","contains":"[Water: 80ml; Milk: 20ml; Coffee: 50ml]:", "price": 10,"$":"ï¿¥"},16 {"id": 3, "name":"3.Mocha","contains":"[Water: 50ml; Milk: 50ml; Coffee: 40ml; Mocha: 10ml]:", "price": 15,"$":"ï¿¥"},17 {"id": 4, "name":"4.Cafe Americano", "contains":" [Water: 100ml; Milk: 0ml; Coffee: 50ml]:","price": 10,"$":"ï¿¥"},18 {"id": 5, "name":"5.Espresso","contains":" [Water: 70ml; Milk: 0ml; Coffee: 80ml]:","price":14,"$":"ï¿¥"},19 {"id": 6, "name":" Quit [Q/q]","contains":" ","price":" ","$":" "}]20 print('##############################################################################\n'21 "##################A nice day starts with a cup of coffee! ####################\n"22 '##############################################################################')23 for menu in menu_list:#ä»åå
¸åå¼24 print(menu.get('name'),menu.get('contains'),menu.get('price'),menu.get('$'))25 26 i = 0#ç¨åéæ¥å¼å¯¼åå循ç¯è¿ç¨ï¼å次循ç¯27 while i < 1: 28 print('#'*78)29 server1 = (input("$ please select your item:(Q/q to quit):"))30 i = i + 131 if server1 == '1':#å¤æåå
¸ä¸å¨åæ°æ®æ¯å¦å¤32 if contains_list["Water"] >= 50 and contains_list["Milk"] >= 50 and contains_list["Coffe"] >= 50:33 price = 12#æå®ä»·æ ¼34 break#è·³åºå¾ªç¯35 else:36 print("your item is inavailable , back to menu")#æ æè¾å
¥è¿åå½æ°37 coffee()38 elif server1 == '2':39 if contains_list["Water"] >= 80 and contains_list["Milk"] >= 20 and contains_list["Coffe"] >= 50:40 price = 1041 break42 else:43 print("your item is inavailable , back to menu")44 coffee()45 elif server1 == '3':46 if contains_list["Water"] >= 50 and contains_list["Milk"] >= 50 and contains_list["Coffe"] >= 40 and contains_list["Mocha"] >= 10:47 price = 1548 break49 else:50 print("your item is inavailable , back to menu")51 coffee()52 elif server1 == '4':53 if contains_list["Water"] >= 100 and contains_list["Coffe"] >= 50:54 price = 1055 break56 else:57 print("your item is inavailable , back to menu")58 coffee()59 elif server1 == '5':60 if contains_list["Water"] >= 70 and contains_list["Coffe"] >= 80:61 price = 1462 break63 else:64 print("your item is inavailable , back to menu")65 coffee()66 elif server1 == 'q':67 exit()#éå¯å½æ°68 elif server1 == 'Q':69 exit()70 else:71 coffee()72 while True:#继ç»æä½å¤æ73 server2 = input("your item is available, please enter 'c' to continue:")74 if server2 == 'c':75 break76 else:77 continue78 #å¤ææ¯å¦ä½¿ç¨ä¼æ å¸79 #使ç¨å½æ°å°è£
ï¼æ¤å¤ä¸ºcoffee()ï¼80 try:#使ç¨tryâ¦â¦finally使å½æ°è·³åºå循ç¯å继ç»æ§è¡ä¹åç代ç 81 while True:#å¤å¾ªç¯82 coupon = input("a coupon is available? [y/n]: ")83 if coupon == 'y':84 while True:#å
循ç¯85 number = input("please enter your coupon No.(Q/q to quit):")86 if number == '666':87 price = float(price*0.6)88 print("you have a coupon with 60% discount, your item price is:")89 print(price)90 return#è·³åºå½æ°91 elif number == '888':92 price = float(price * 0.8)93 print("you have a coupon with 80% discount, your item price is:")94 print(price)95 return96 elif number == 'q':97 break#è·³åºå
循ç¯ï¼ç»§ç»å¤å¾ªç¯98 elif number == 'Q':99 break100 else:101 print("wrong number!")102 continue#继ç»å
循ç¯103 elif coupon == 'n':104 print("you have a coupon with no discount, your item price is:")105 print(price)106 break#è·³åºå¤å¾ªç¯ï¼ç»§ç»å½æ°107 else:108 continue#继ç»å
循ç¯109 finally:#è·³åºå继ç»å½æ°110 #å¤æéé¢111 cash = float(0)112 if cash <= price:#é¦æ¬¡æ¡ä»¶å¤æ113 cash20 = float(input("please enter your cash, how many 20ï¿¥count? :"))114 cash10 = float(input("how many 10ï¿¥count? :"))115 cash5 = float(input("how many 5ï¿¥count? :"))116 cash1 = float(input("how many 1ï¿¥count? :"))117 cash = cash1 + cash5*5 + cash10*10 + cash20*20 + cash118 print("your total amount is: " + str(cash))119 while True:120 if cash <= price:#循ç¯æ¡ä»¶å¤æ121 server3 = input("you have not enough money amount for buying, please continue to enter your cash [c] or quit with [q]: ")122 if server3 == 'c':#循ç¯123 cash20 = float(input("how many 20ï¿¥count? :"))124 cash10 = float(input("how many 10ï¿¥count? :"))125 cash5 = float(input("how many 5ï¿¥count? :"))126 cash1 = float(input("how many 1ï¿¥count? :"))127 cash = cash1 + cash5*5 + cash10*10 + cash20*20 + cash128 print("your total amount is: 1" + str(cash))129 continue130 elif server3 == 'q':131 coffee()132 elif server3 == 'Q':133 coffee()134 elif cash > price:#å¤ææååï¼æå°ç»æè¯å¥135 charge = float(cash - price)136 print("your change is: "+ str(charge) + " please receipt it. ")137 print("your coffee is making:\n"138 "â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦.\n"139 "## coffee preparing finished!\n"140 "â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦.\n"141 "## coffee making finished!\n"142 "â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦â¦.\n"143 "## coffee packaging finished!\n\n\n\n\n")144 145 break146 #è®°å½å®åæ
åµ147 if server1 == '1':148 kinds_list["Cappuccino"] = kinds_list["Cappuccino"] + 1149 elif server1 == '2':150 kinds_list["Latte"] = kinds_list["Latte"] + 1151 elif server1 == '3':152 kinds_list["Mocha"] = kinds_list["Mocha"] + 1153 elif server1 == '4':154 kinds_list["CafeAmericano"] = kinds_list["CafeAmericano"] + 1155 elif server1 == '5':156 kinds_list["Espresso"] = kinds_list["Espresso"] + 1157 print("The items sold on this machine are:\n"158 "# Cappuccino: " + str(kinds_list["Cappuccino"]) +"\n"159 "# Latte: " + str(kinds_list["Latte"]) +"\n"160 "# Mocha: " + str(kinds_list["Mocha"]) +"\n"161 "# Cafe Americano: " + str(kinds_list["CafeAmericano"]) +"\n"162 "# Espresso: " + str(kinds_list["Espresso"]) +"\n"163 "### Total profile:"+ str(price))164 #æç»ç¡®å®æ´æ¹åå
¸ä¸çæ°æ®165 if server1 == '1':166 contains_list["Water"] = contains_list["Water"] - 50167 contains_list["Milk"] = contains_list["Milk"] - 50168 contains_list["Coffe"] = contains_list["Coffe"] - 50169 elif server1 == '2':170 contains_list["Water"] = contains_list["Water"] - 80171 contains_list["Milk"] = contains_list["Milk"] - 20172 contains_list["Coffe"] = contains_list["Coffe"] - 50173 elif server1 == '3':174 contains_list["Water"] = contains_list["Water"] - 50175 contains_list["Milk"] = contains_list["Milk"] - 50176 contains_list["Coffe"] = contains_list["Coffe"] - 40177 contains_list["Mocha"] = contains_list["Mocha"] - 10178 elif server1 == '4':179 contains_list["Water"] = contains_list["Water"] - 100180 contains_list["Coffe"] = contains_list["Coffe"] - 50181 elif server1 == '5':182 contains_list["Water"] = contains_list["Water"] - 70183 contains_list["Coffe"] = contains_list["Coffe"] - 80184 coffee()#å°æ°æ®ä¼ å°ä¸ä¸ªå¾ªç¯185coffee()#第ä¸æ¬¡æ§è¡å½æ°186#æ¶è·187#1.å
ç¨å½æ°å°è£
ï¼å使ç¨tryâ¦finallyæ¥ä½¿è·³åºå循ä¸ç»æçé¨å继ç»è¿è¡ä¹åç代ç ï¼è¿æ ·åè·³ååè·³é½è½å¨åä¸å½æ°çåä¸åé循ç¯éè¿è¡ã188#2.åå
¸ç¡®å®é®å¼å¯¹ï¼ç¶åæå°å½æ°ä¸å¾ªç¯ä½¿ç¨å°±å¯ä»¥äºãååéåå
ç»é½ä¸å¯ä»¥æ´æ¹å°åï¼ä½æ¯å表æè
åå
¸å¯ä»¥ï¼æ以就è½ä¿çä¸ä¸æ¬¡å¾ªç¯çç»æã189#3.ä¸å¯æ´æ¹æ°å¼æ´æ¢çæ¯å°åæåï¼ç§°ä¸ºå¼ä¼ éã190#4.å¦å¤ä¸ç§æ¹æ³å°±æ¯ææ°æ®å¨åå°åºä¸ï¼å¨ä½¿ç¨æ¶è°ç¨æ°æ®åºä¸éä¸éè¦åï¼è¿æ ·å°±å¯ä»¥æ°¸ä¹
æ§æ´æ¹æ°å¼ãæ³æ¢å¤æ°æ®çè¯å°±æ´ä¸ªæ¢å¤ä¿åçåå§æ°æ®åºã191#æ»ç»ï¼æä¸å®é
åæ°aï¼å¨è°ç¨æ¶å产çä¸ä¸ªæ°çåéxï¼å½¢å¼åæ°ï¼ï¼xåaæåç¸åçå°åãå¦æ对xèµå¼ï¼æå³çæ¹åäºxæåçå
ååï¼èä¸æ¹åaçå¼ã...
day_07.py
Source:day_07.py
1import os23curr_dir = os.path.dirname(os.path.abspath(__file__))45f = open(os.path.join(curr_dir, "data.txt"))6# f = open(os.path.join(curr_dir, "test_part_2.txt"))7txt = f.readlines()8f.close()910txt_cleaned = [item.strip() for item in txt]1112# print(txt_cleaned)1314bag_rule_dict = {}1516for line in txt_cleaned:17 rule_components = line.split("contain")18 own_color = rule_components[0][:-6]1920 contains_list_input = rule_components[1].strip().split(", ")21 contains_list_output = []2223 for content in contains_list_input:24 # print(content)2526 if content == "no other bags.":27 contains_list_output = None28 else:29 content_split = content.split()30 quantity = int(content_split[0])31 bag_color = f"{content_split[1]} {content_split[2]}"3233 contains_list_output.append({"color": bag_color, "quantity": quantity})3435 bag_rule_dict[own_color] = contains_list_output363738class bag:39 def __init__(self, color, rule_dict):40 self.own_color = color41 self.contains_list = []4243 self.contains_list = rule_dict[color]4445 def get_own_color(self):46 return self.own_color4748 def fill_bag(self, rule_dict):49 if not self.contains_list is None:50 for item in self.contains_list:51 item["bag"] = bag(item["color"], rule_dict)52 item["bag"].fill_bag(rule_dict)5354 def is_color_in_bag(self, color):55 answer = False5657 if self.contains_list is None:58 pass59 else:60 for entry in self.contains_list:61 if entry["color"] == color:62 answer = True6364 if answer == False:65 if self.contains_list is None:66 pass67 else:68 for entry in self.contains_list:69 if entry["bag"].is_color_in_bag(color):70 answer = True71 break7273 return answer7475 def number_bags_with_self(self):76 number = 17778 if self.contains_list is None:79 pass80 else:81 for entry in self.contains_list:82 number += entry["quantity"] * entry["bag"].number_bags_with_self()8384 return number858687bag_list = []8889for bag_color in bag_rule_dict:90 # print(bag_color)91 bag_n = bag(bag_color, bag_rule_dict)92 bag_n.fill_bag(bag_rule_dict)93 bag_list.append(bag_n)9495# Part 1.96number_bags = 09798for bag in bag_list:99 if bag.is_color_in_bag("shiny gold"):100 number_bags += 1101102print(f"Number of bags containing shiny gold: {number_bags}")103104# Part 2.105106for bag in bag_list:107 if bag.get_own_color() == "shiny gold":108 number_contained_bags = bag.number_bags_with_self()109
...
finder.py
Source:finder.py
1import json2# load words from json file3with open('words_5_letters.json', 'r', encoding='utf-8') as f:4 words = json.load(f)5 persian_words = words['vaajoor']6 english_words = words['wordle']7def word_finder(game_mode: str, exact_dict: dict, contains_list: list, not_contains_list: list):8 if len(contains_list) == 0 and len(exact_dict.keys()) == 0 and len(not_contains_list) == 0:9 return {'message': 'No condition', 'result': [], 'result_count': 0}10 condition = ''11 if len(exact_dict.keys()) > 0:12 condition = ' and '.join([f'word[{key}] == "{exact_dict[key]}"' for key in exact_dict])13 if len(contains_list) > 0 and len(exact_dict.keys()) > 0:14 condition += ' and '15 if len(contains_list) > 0:16 condition += ' and '.join([f'"{character}" in word' for character in contains_list])17 if len(not_contains_list) > 0 and (len(exact_dict.keys()) > 0 or len(contains_list) > 0):18 condition += ' and '19 if len(not_contains_list) > 0:20 condition += ' and '.join([f'"{character}" not in word' for character in not_contains_list])21 if game_mode == 'vaajoor':22 words_dictionary = persian_words23 elif game_mode == 'wordle':24 words_dictionary = english_words25 else:26 return {'message': 'Mode not acceptable', 'result': [], 'result_count': 0}27 all_acceptable_words = []28 for word in words_dictionary:29 if eval(condition):30 all_acceptable_words.append(word)31 if len(all_acceptable_words) == 0:32 return {'message': 'No words were found', 'result': [], 'result_count': 0}...
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!!