mercredi 11 mars 2020

Trying out Python. A friend gave me a problem and i don't want to cheat [duplicate]

Given a list of numbers and a number K, return whether any two numbers from the list add up to K.

Example:

[10, 15, 3, 7] and K = 17, return True, since 10 + 7= 17.

so I ended up making this:

mylist1  = int
mylist2  = int
k = int

import random

mylist = [mylist1, mylist2]

for i in range(0, 100):
    x = random.randint(1, 10)
    mylist.append(k)

import random

mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

k = random.sample(mylist, 2)

print sum (k)

if sum == mylist: print ("true")

I just want to know, what is it that I'm doing wrong. Can I just double a random number to get the same effect of adding random numbers? Also, how do you loop adding random numbers continuously until you get a matching outcome? thanks.




Aucun commentaire:

Enregistrer un commentaire