Problem using print to show function result

Asked

Viewed 659 times

0

I’m creating a function that scrambles and returns the typed word, but I don’t know how to use the print (if this is really 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".'''
  • 1

    First of all, it helps to know how are you studying, so that we can understand what is no longer very effective, so that we can finally diversify ideas and try to help.

  • Well, I’m preferring to study for online (free) courses, since most of Python’s books are in English. Of the subjects I have studied, I am sure I have learned only logical operators, variables, repetition structures, control structures, strings and lists. And whenever I’ve learned one of these I’ve moved on to the next, but when there are some more specific commands like the Sorted or the Random library resources that are not always introduced in online courses, or even just understand why a certain error occurred, then I begin to see my difficulties.

  • I reviewed everything I thought I knew, I chose other online courses to study, and even if I want to do an exercise (like the question) I do not advance until I know everything I need to do it. I found a good book about Python Basic in Portuguese and I’m reading it, I think I’m on the right track now.

2 answers

4

What you want to do is sort and not shuffle. Or if you want to shuffle is wrong.

Just do this:

print(''.join(sorted(input())))

I put in the Github for future reference.

Of course this shape is not robust, but the original did not worry about robustness. Any wrong typing and does not work.

1


The problem is actually in:

 return a.sort()

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

If you want to print the ordered 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())

Browser other questions tagged

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