Python returns None when using "Sort" list method

Asked

Viewed 29 times

0

I need to create a list that will be created by numbers entered by the user and, when inserting repeated numbers, this number should not be added. Finally, the program must present the list with the numbers in ascending order. I did the following but present at the end as a result None. Also, I am having difficulty inserting a check inside the loop to ask for another answer if the user does not type’s' or 'n' in 'Want to continue?'.

numeros = list()

while True:
    n = int(input('Digite um número:'))
    if n in numeros:
        n = int(input('O número já foi digitado! Digite outro:'))
    else:
        numeros.append(n)
    r = input('Deseja continuar? [S/N]:')
    if r.upper() == 'N':
        break

print(numeros.sort())
  • 1

    Your algorithm looks right. The problem is that the method sort of the lists simply modifies (ordering it) the list and returns nothing. If you want the list to be properly ordered and returned, use the function sorted, thus: print(sorted(numeros)).

  • As for checking, you could try something like: while not r in 'SsNn':
 r = input('Entrada Inválida. Digite S ou N: ')

No answers

Browser other questions tagged

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