Вместо того, чтобы брать по одной карте за раз, вы можете просто перетасовать все свои карты и всегда pop()
первую - это лучше, чем выбрать одну случайную и впоследствии получить ее индекс:
import random
cards = [.5,1,2,3,4,5,6,7]
# shuffle the whole cards list into a new random configuration
pickings = random.sample(cards,k=len(cards))
s = 0
c = []
while True: # they eventually add up to more then 7.5 so no need to check for empty list
card = pickings.pop(0) # get first
if s + card < 7.5: # can add?
s += card # do so
c.append(card) # and to list so we see the cards collected
else:
print("cant take ", card) # cant, break while
break
print ("Sum:",s)
print(c)
Некоторые пробеги:
cant take 4
Sum: 3.5
[3, 0.5]
cant take 3
Sum: 5
[4, 1]
cant take 7
Sum: 2.5
[0.5, 2]