print([[x, y, z] for x in range(3) for y in range(3) for z in range(3)])
# ну или на привычных циклах
res = []
for x in range(3):
for y in range(3):
for z in range(3):
res.append([x, y, z])
print(res)
import random
import itertools
def take_coins(max_coins):
while True:
raw_coins = input('возьмите монеты[{}..{}]'.format(1, max_coins))
try:
coins = int(raw_coins)
if not 1 <= coins <= max_coins:
raise ValueError()
except ValueError as ex:
print('неверные данные')
else:
return coins
def game(coins, players):
players = random.sample(players, len(players))
for player in itertools.cycle(players):
if coins == 0:
print('игрок {} проиграл'.format(player))
break
else:
print('{} ходит'.format(player))
coins -= take_coins(min(10, coins))
print('осталось {} монет'.format(coins))
if __name__ == '__main__':
game(coins=100, players=['Вася', 'Петя'])
An offer listing ID is an alphanumeric token that uniquely identifies an item. Use the OfferListingId instead of an item's ASIN to add the item to the cart.
Type: String
Default: None
Valid Values: An Offer Listing ID
Constraint: Required if ASIN is not used.
# bad
b = 0
for item in lastnames:
lastname = lastnames[b]
b = b + 1
print(lastname)
# good
for lastname in lastnames:
print(lastname)
firstnames = ['Andrey', "Sasha", "Artem", "Roma"]
lastnames = [" veskii"," Kross"," Lylka"]
# можно так
for lastname in lastnames:
with open('{}.txt'.format(lastname), 'w') as f:
for i, firstname in enumerate(firstnames):
f.write('{} {} {}\n'.format(i, firstname, lastname))
# или так
names = dict((lastname, firstnames) for lastname in lastnames)
for lastname, names_list in names.items():
with open('{}.txt'.format(lastname), 'w') as f:
f.write(
'\n'.join(
'{} {} {}'.format(i, firstname, lastname) for i, firstname in enumerate(names_list)
)
)
def get_max(text):
count_map = {char:text.count(char) for char in set(text)}
return max(count_map, key=lambda char: count_map[char])
import json
def foo(host, port):
....
with open('config.json', 'r') as f:
config = json.loads(f.read())
foo(config['host'], config['port']) # можно так
foo(**config) # или так
import sys
import time
import Queue
import threading
import random
MAX_THREADS = 5
def start_thread(*args, **kwargs):
t = threading.Thread(*args, **kwargs)
t.daemon = True
t.start()
return t
def dumb_print(q):
line = q.get()
time.sleep(random.randint(1, 10))
sys.stdout.write(line)
q.task_done()
def main_loop():
q = Queue.Queue(MAX_THREADS)
with open('strings.txt', 'r') as f:
data = f.readlines()
for line in data:
q.put(line)
start_thread(target=dumb_print, args=(q,))
q.join()
if __name__ == '__main__':
sys.stdout.write('start\n')
loop = start_thread(target=main_loop)
while loop.is_alive():
sys.stdout.write('still alive\n')
time.sleep(random.randint(1, 5))
sys.stdout.write('done\n')