1

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: '))
        list1.append(x)
    return list1


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


formatFunc(listFunc())
3
  • 1
    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. Commented Nov 4, 2018 at 4:44
  • 1
    You are comparing num instead of x which is the user input. Commented Nov 4, 2018 at 4:44
  • 1
    Receive input to num. Also ' '.join(final) does not change anything unless you assign back to final. Commented Nov 4, 2018 at 4:44

3 Answers 3

2

It should be the same variable used in while loop.

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

and

# final = str(y) This is not required, why cast list as str
final = ' '.join(final)
Sign up to request clarification or add additional context in comments.

6 Comments

thanks Vishnudev - the output is still a comma seperated list (with a space now) but this gets me closer to what I am expecting
@JD2775 Did you notice the commented line
I did, thanks. When I take it out it errors saying expecting str instance, int found
Put str(num) while appending
Bingo! That did it. Thank you!
|
1
x = int(input('Enter a number, -999 to quit: '))
list1.append(x)    
num=x

will work!

Comments

1

You are calling the functions correctly if you intend on printing the inputs of listfunc. However the inputs will not be saved to a variable in global scope and will thus be locked out from any future use.

Additionally, listfunc currently does no input validation. It is possible to input any strings in the input. The while loop doesn't end because the condition in the while is never met. Rewriting it according to your conditions yields:

def listfunc():
        someList = []
        while True:
                x = input("Enter a number, exit to quit")
                if 'exit' in x.lower():
                    break
                elif x.isdigit():
                    someList.append(x)
                else:
                    print("Input not recognized try again")
        return someList

def formatFunc(v):
        print(''.join(str(i) + ' ' for i in v)

Do you see why this works?

3 Comments

Thanks @GlobalTraveler. The input validation piece makes sense, and I need something to break out of the loop like you showed. I am confused though on your mentioning the "inputs will not be saved to a variable in global scope and will thus be locked out from any future use". I am not sure I understand it. it looks like in your version you are defining and returning someList [] in the listFunc. Same as I am doing. Is there something there that makes it globally accessible, or are you just mentioning that as a warning to me with the structure overall and a need to move it out of the func?
Assuming you want to reuse the inputs the user entered you would like to store the results to a list, i.e. inputs = listfunc(); formatfunc(inputs). Then the inputs can be reused for other purposes. It is a minor point and not relevant to your initial question.
Ah I see what you are saying. I just tried it. That makes perfect sense and clears up some confusion I actually have had for a while (not related to this question). Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.