Pycharm and REPL-Newbie

Hi,

I’m trying to learn generators and iterators. The code I have written is very simple for just understanding the concepts. My program works on REPL but not in pycharm. Generator object is getting created but when I use next() it is not giving out any value. Tried using print to display the value but it Just printing out generator object. Please help me to understand what I’m doing wrong here. TIA

def count(n=0):
while True:
yield n
n += 1

c = count(0)
print(c)

next(c)
print(c)
next(c)
print(c)
next(c)
print(c)

Your Python code shows messed up. You have to enclose it between triple backticks to prevent Markdown from mangling it:

```
Your code will be here.
```

REPL shows the result of the evaluated expression for you. In normal Python code if you want to show the result you have to write it explicitly to the terminal. For example in your code it could be:

print(next(c))
1 Like

Thank you. Next time will use triple backticks

1 Like