Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Not able to make function to add element to list. While loop doesn't terminate. 'NONE' is printed

+4 votes
asked Apr 12, 2023 by VikasArya112233 (160 points)
def CreateList(list):
    choice=1
    while(choice!=0):
        choice=input(print('enter choice \n 0 to stop adding elements to list \n 1 to add element to list : \n'))
        if(choice==1):
            list.append(print('enter element to add to the list : '))
    return list    
    
    
print('list before :', list)
list=CreateList(list)
print('list after : ',list)

4 Answers

0 votes
answered Apr 23, 2023 by 쫄라맨과 초록 종이 (240 points)
oh... Did you make it with "Python3" ?
0 votes
answered Apr 30, 2023 by X T (140 points)
1st issue: choice value enter by user need to cast to int

2nd issue: in the append parenthesis is the input() not print()

3rd issue: list variable must first be created as: list = [] before the call to print('list before', list)

lastly, you really not need return statement in the CreateList() and no need to assign return value from CreateList() to list.
0 votes
answered May 1, 2023 by Ramesh (180 points)
Following would be the right code:

def CreateList(lst):
    choice=1
    while(choice!=0):
        print('0 to stop adding elements to list \n1 to add element to list : \n')
        choice=int(input('Enter choice'))
        if(choice==1):
            lst.append(int(input('enter element to add to the list : ')))
        elif(choice==0):
            pass
        else:
            print('Invalid Choice\n')
    return lst    
    
lst = []
print('list before :', lst)
lst=CreateList(lst)
print('list after : ',lst)
0 votes
answered May 2, 2023 by Peter Minarik (86,240 points)

Here's your code fixed:

def CreateList(list):
    choice = 1
    while choice != 0:
        choice = int(input('enter choice\n 0 to stop adding elements to list\n 1 to add element to list\n: '))
        if choice == 1:
            list.append(int(input('enter element to add to the list : ')))
    
list = []
print('list before :', list)
CreateList(list)
print('list after: ', list)

Some highlights:

  1. the input() function can take a prompt to be printed before it reads data from the user.
  2. In your code choice = input(print('...')) you put the return value of print() as the prompt for input(), hence "None" was displayed on the console as print() does not return anything.
  3. You did not initialize your list to be an empty list
  4. You don't need to assign list the return value of CreateList() as you already have the list passed in in the argument list and you can modify it inside the function.
  5. You don't need to put parenthesis around the condition in while and if. You can have as many parentheses as you want, but it works fine without them. :)
Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...