The input by default will be in string format. So when you read input in newList
, its value is : '1'
. So the below code would work.
oldList = ['1', '2']
newList = input('Which number should be included in the List?')
if newList == '1': # and not 1
oldList.append(1)
elif newList == '2':
oldList.append(2)
print(oldList)
Input : 1
Output :
Which number should be included in the List?
['1', '2', 1]
You could instead also try keeping your same comparison and just converting newList
into int. That would work as well.
NOTE : The above code will append an integer to oldList
. So, if you want to append string , you should change the code to oldList.append(str(1))
.
One more thing, if you just want to append a number which user inputs, you can use this -
Short-hand Version :
oldList = ['1', '2']
oldList.append(int(input('Which number should be included in the List?')))
print(oldList)