List ordering returns "None"

Asked

Viewed 82 times

1

I’m doing a program that renders keys to the euro millions, but the keys are None, what is my mistake?

import random
print('Chave do Euromilhões')

num= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
estr=[1,2,3,4,5,6,7,8,9,10,11]


while  True:
    n1= random.randint(1,50)
    if n1 in num:
        #print(n1, end=', ')
        break
while  True:
    n2= random.randint(1,50)
    if n2 in num and n1!=n2:
        #print(n2, end=', ')
        break
while  True:
    n3= random.randint(1,50)
    if n3 in num and n1!=n3 and n2!=n3:
        #print(n3, end=', ')
        break
while  True:
    n4= random.randint(1,50)
    if n4 in num and n1!=n4 and n2!=n4 and n3!=n4:
        #print(n4, end=', ')
        break
while  True:
    n5= random.randint(1,50)
    if n5 in num and n1!=n4 and n2!=n5 and n3!=n5 and n4!=n5:
        #print(n5, end='. ')
        break

Números= [n1,n2,n3,n4,n5]

Números=Números.sort(reverse= False)

print(Números)


while  True:
    e1= random.randint(1,11)
    if e1 in num:
        #print(e1, end=', ')
        break
while  True:
    e2= random.randint(1,11)
    if e2 in num and e1!=e2:
        #print(e2, end='. ')
        break

Estrelas= [e1,e2]
Estrelas=Estrelas.sort(reverse= False)


print('Os números do euromilhões são:',Números)
print('As estrelas são:', Estrelas)

2 answers

2


The method sort sorts the list in-place (that is, modifies the list itself instead of returning another). And its return is None, so it makes no sense to assign your result to a variable.

So instead of:

Números = Números.sort(reverse=False)

Just do:

Números.sort(reverse=False)

The same goes for:

Estrelas.sort(reverse=False)

Or if you prefer, create the already ordered lists using sorted:

Números = sorted([n1,n2,n3,n4,n5], reverse=False)

Estrelas = sorted([e1,e2], reverse=False)

Although the value default of reverse already is False, then it could just be:

Números.sort()
Estrelas.sort()

# ou
Números = sorted([n1,n2,n3,n4,n5])
Estrelas = sorted([e1,e2])

Not directly related, but you can simplify that lot of while thus:

import random
print('Chave do Euromilhões')

numeros_possiveis = range(1, 51)
estrelas_possiveis = range(1, 12)

Números = sorted(random.sample(numeros_possiveis, 5))
Estrelas = sorted(random.sample(estrelas_possiveis, 2))

print('Os números do euromilhões são:',Números)
print('As estrelas são:', Estrelas)

range creates a sequence of numbers (remembering that the final value is not included, so I put 51 and 12) and random.sample picks a certain number from the given sequence (and already ensures that they will not be repeated).

  • I had done it initially, but it wasn’t working for me, but thanks anyway, it worked!

1

The method list.sort() just sort the list in-place and always returns None.

An alternative would be to use the function sorted(), that returns an ordered copy of the list and does exactly what you want, for example:

Numeros = sorted(Numeros)
Estrelas = sorted(Estrelas)

To generate a list of integers sequentially, Voce could use the generating function range(), look at you:

num = list(range(1, 10 + 1))
print(num)

Exit:

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

Instead of you using the brute force to draw a draw n of elements of a list without them repeating, you can use the method random.sample(), something like that:

import random

x = random.sample([1,2,3,4,5,6,7,8,9,10], k=5)
print(x)

Possible Exit:

[9, 6, 7, 3, 8]

Considering all that has been said, your program could be rewritten as follows:

from random import sample

Numeros = sorted(sample(range(1, 50 + 1), 5))
Estrelas = sorted(sample(range(1, 11 + 1), 2))

print('Os numeros do euromilhoes sao: ', Numeros)
print('As estrelas sao: ', Estrelas)

Possible Exit:

Os numeros do euromilhoes sao:  [1, 13, 16, 41, 45]
As estrelas sao:  [1, 6]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.