Home > Enterprise >  PyCharm loops one less than expected and adds empty spaces to list inexplicably
PyCharm loops one less than expected and adds empty spaces to list inexplicably

Time:06-02

In this code I want to enter elements in the list and display the list as ouput. but I am getting different output than desired. When I entered 4 elements, it allows me to enter only three and gives output only 2 elements while there is 2 empty elements. Can you help? I am using Pycharm.

g = int(input("Enter the number of elements: "))
fruit_list = []
print("Enter the elements: ")
i = 0
while i < g:
    fruit_list.append(input())
    i  = 1

print(fruit_list)

OUTPUT

Enter the number of elements: 4
Enter the elements: 
John
Bill
Sam
['John', '', 'Bill', '']

CodePudding user response:

This issue can be resolved in PyCharm by editing the Run/Debug settings. Under 'Execution', select 'Run with Python Console'.

This would not be an issue if executed outside of PyCharm - i.e., direct invocation of python

  • Related