Return value used in another function - Python

3690 views python

I want to write 2 functions. One function that takes input from a user and adds it to a list. The 2nd function takes the list returned from the 1st function and prints out each element separated by a space. I think I am close, but something isn't right. Typing -999 doesn't stop the loop, and I can't tell if I am calling the functions correctly...

Any ideas?

def listFunc():
    num = 0
    list1 = []
    while num != -999:
        x = int(input('Enter a number, -999 to quit: '))
    return list1

def formatFunc(y):
    final = str(y)
    ' '.join(final)


answered question

You set num=0 and don't update it inside the loop. If you were to straighten out the variable names between num and x, you would see a different result I suspect. Voting to close as typo.

You are comparing num instead of x which is the user input.

Receive input to num. Also ' '.join(final) does not change anything unless you assign back to final.

2 Answers


It should be the same variable used in while loop.

num = int(input('Enter a number, -999 to quit: '))
if num != -999:


# final = str(y) This is not required, why cast list as str
final = ' '.join(final)

posted this

x = int(input('Enter a number, -999 to quit: '))

will work!

posted this

Have an answer?


Please login first before posting an answer.