简体   繁体   中英

Python - why isnt my function outputting 'a' ten times? Beginner question

def testfunction():
    for i in range(10):
        return('a')


print(testfunction())

I want 'a' outputed 10 times in one line. If I use print instead of return, it gives me 10 'a's but each on a new line. Can you help?

return terminates the current function, while print is a call to another function(atleast in python 3)

Any code after a return statement will not be run. Python's way of printing 10 a's would be:

print('a' * 10)

In your case it would look like the following:

def testfunction ():
    return 'a' * 10

print(testfunction ())

The reason its only printing once is because the return statment finishes the function (the return function stops the loop).

In order to print 'a' 10 times you want to do the following:

def testfunction():
    for i in range(10):
        print('a')


testfunction()

If you want "a" printed 10 times in one single line then you can simply go for:

def TestCode():
       print("a"*10)

There's no need to use the for loop. For loop will just "a" for 10 times but every time it'll be a new line.

You can also take in a function argument and get "a" printed as many times as desired. Such as:

def TestCode(times):
       t = "a"*times
       print(t)

Test:

TestCode(5)
>>> aaaaa

TestCode(7)
>>> aaaaaaa

print and return get mixed up when starting Python.

A function can return anything but it doesn't mean that the value will be printed for you to see. A function can even return another function (it's called functional programming).

The function below is adapted from your question and it returns a string object. When you call the function, it returns the string object into the variable called x . That contains all of the info you wanted and you can print that to the console.

You could have also used yield or print in your for loop but that may be outside of the scope.

def test_function(item:str="a", n:int=10):
    line = item*n # this will be a string object
    return line

ten_a_letters = test_function()
print(ten_a_letters)
"aaaaaaaaaa"

two_b_letters = test_function("b",2)
print(two_b_letters)
"bb"

I want 'a' outputed 10 times in one line. If I use print instead of return, it gives me 10 'a's but each on a new line.

If you want to use print , the you need to pass a 2nd parameter as follows:

def testfunction():
    for i in range(10):
        print('a', end='')

However, I think the pythonic way would be to do the following:

def testfunction():
    print('a' * 10)

When you use return you end the execution of the function immediately and only one value is returned.
Other answers here provide an easier way to solve your problem (which is great), but I would like to suggest a different approach using yield (instead of return ) and create a generator (which might be an overkill but a valid alternative nonetheless):

def testfunction():
    for i in range(10):
        yield('a')

print(''.join(x for x in testfunction()))

1. What does "yield" keyword do?

def test ():
    print('a' * 10)

test()

Output will be 'aaaaaaaaaa'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM