Problem using print to show function result

0

I'm creating a function that shuffles and returns the typed word, but I do not know how to use print (if that's the case) to show the result of the function below:

def f():
    a = []
    x = 0
    while x < len(word):
        a.append(word[x])
        x += 1
        if x > len(word):
            break
    return a.sort()



word = input()
f()
'''Aqui ficaria o print. Seria algo parecido com "print(f())"? Tentei exatamente isso e a resposta foi "None".'''
    
asked by anonymous 21.01.2018 / 21:50

2 answers

1

The problem is actually in:

 return a.sort()

The method sort() it sorts the list in ascending order but it does not return any value ( None ). So when you try to print the return of the f() function you just print your return: None .

If you want to print the sorted list, your code would look like this:

def f():
    a = []
    x = 0
    while x < len(word):
        a.append(word[x])
        x += 1

    a.sort()    
    return a



word = input()
print(f())
    
21.01.2018 / 22:04
4

What you're trying to do is sort and not shuffle. Or if you want to shuffle it is wrong.

Just do this:

print(''.join(sorted(input())))
    
21.01.2018 / 21:58