[Solved] Sum from random list in python

There is a function combinations in itertools and it can be used to generate combinations. import random import itertools # Generate a random list of 30 numbers mylist = random.sample(range(-50,50), 30) combins = itertools.combinations(mylist, 3) interested = list(filter(lambda combin: sum(combin) == 0, combins)) print(interested) Note that the results from filter() and itertools.combinations() are iterables and … Read more

[Solved] Game code error line skipping – Python [closed]

I think the problem is here: r = str(input(‘{} please enter a integer between 1 and 10: ‘.format(name))) r = str(input(‘{} please enter a integer between 1 and 10: ‘.format(name2))) Don’t you mean to assign to p the second time through? p = str(… Also, these lines don’t make much sense: elif (r > ‘r1’ … Read more

[Solved] how do I convert array to JSON

>>> import json >>> url_list = [‘http://www.google.com’, ‘http://www.yahoo.com’] >>> json.dumps({‘entry’: [{‘url’: v} for v in url_list]}) ‘{“entry”: [{“url”: “http://www.google.com”}, {“url”: “http://www.yahoo.com”}]}’ >>> print json.dumps({‘entry’: [{‘url’: v} for v in url_list]}, indent=4) { “entry”: [ { “url”: “http://www.google.com” }, { “url”: “http://www.yahoo.com” } ] } The amount of whitespace isn’t significant in json. If you want … Read more

[Solved] How can I make my program input images taken from a Camera? [closed]

You can capture a single frame by using the VideoCapture method of OpenCV. import cv2 pic = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop) ret,frame = pic.read() # return a single frame in variable `frame` while(True): cv2.imshow(‘img1’,frame) #display the captured image if cv2.waitKey(1) & 0xFF == ord(‘y’): #save on pressing ‘y’ cv2.imwrite(‘images/c1.png’,frame) … Read more

[Solved] How do I go back to a variable in python? [closed]

Maybe you want: while True: … This will put you back at the top if you place the statement at the top of your script. Like this: while True: command=raw_input(“j;/”) command1=”help” command2=”jver” if command==command1: print “List of commands” print”” print”” print”help = shows all commands” print “” print “jver = Version of MS-Josh” elif command==command2: … Read more

[Solved] Code not working (python) [closed]

if d<r: vx,vy,ax,ay=0,0,0,0 return [vx,ax,vy,ay] This isn’t a function so you can’t use return. If you want to add those values to a list you can make a new list: new_list = [vx,ax,vy,ay] You don’t need to use return outside of a function because those variables are already in scope. When you use variables inside … Read more

[Solved] I don’t know what to do next [closed]

I’ll help you with some parts, but most of this code should be a learning experience for you. import datetime # prompt here for the date # put it into a datetime.datetime object named “day” # this is the part of the code you need to type day_array = [“Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”] day_of_week = day_array[day.weekday()] The datetime … Read more

[Solved] How would I make a simple encryption/decryption program? [closed]

Use two dicts to do the mapping, one from letters to encryption_code and the reverse to decrypt: letters=”ABCDEFGHIJKLMNOPQRSTUVWXYZ” encryption_code=”LFWOAYUISVKMNXPBDCRJTQEGHZ” enc = dict(zip(letters,encryption_code)) dec = dict(zip(encryption_code, letters)) s = “HELLO WORLD” encr = “”.join([enc.get(ch, ch) for ch in s]) decr = “”.join([dec.get(ch, ch) for ch in encr]) print(encr) print(decr) Output: IAMMP EPCMO HELLO WORLD Using your … Read more