Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple counter
#1
I am trying to make a simple counter that starts at 1 and increments by 1 indefinitely.
I have tried for-loops but I get int object not iterable.
I have tried while loops with i += 1.
i = 1
while True:
    print(i += 1),
I get...
Error:
File "count.py", line 3 print(i += 1), ^
Why is counting so hard in Python?
Reply
#2
There is an unneeded comma after the print. It is not hard, but you can't do an assignment in a function call. Do the increment before calling print.
That is what the error message says, and those messages are there to help you debug ;)
Reply
#3
(Feb-03-2018, 09:32 AM)j.crater Wrote: There is an unneeded comma after the print. It is not hard, but you can't do an assignment in a function call. Do the increment before calling print.
I get syntax errors without the comma!
Error:
File "count.py", line 3 print(i += 1) ^ SyntaxError: invalid syntax
Reply
#4
This is what I mean:
i = 1
while True:
    i += 1
    print(i)
Reply
#5
(Feb-03-2018, 09:34 AM)j.crater Wrote: There is an unneeded comma after the print. It is not hard, but you can't do an assignment in a function call. Do the increment before calling print.
That is what the error message says, and those messages are there to help you debug ;)
def count():
    i = 1
    while True:
        i += 1
print(count())
I get a blinking cursor.
How do I do the increment before the print?
Reply
#6
(Feb-03-2018, 09:47 AM)mcmxl22 Wrote: I get a blinking cursor.
The function call never returns because it is trapped in the while loop. You need to find a way
to exit the while loop.
Reply
#7
This was my final solution.
import time


def earnings():
    i = 1
    while True:
        i += 1
        time.sleep(1)
        print(i, end='\r')
		
earnings()
Reply
#8
It never stop Sleepy
Here a version you can look at.
import  time

def count(start, stop):
    i = start-1
    while True:
        time.sleep(1)
        i += 1
        print(i)
        if i == stop:
            return f"Reached stop at <{stop}>,i'm out"

print(count(1, 10))
Reply
#9
(Feb-03-2018, 09:52 PM)snippsat Wrote: It never stop
I don't want it to stop. I want to figure out how to control the speed so I can create a game similar to this.
Reply
#10
(Feb-03-2018, 10:21 PM)mcmxl22 Wrote: I don't want it to stop. I want to figure out how to control the speed so I can create a game similar to this.
Sure all Game,GUI,Web(server) has some kind of main loop that run stuff.
In example PyGame or Tkinter there is way to get out.
# --- Main Pygame Loop ---
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
# A quit button
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"

# --- Main Tkinter Loop ---
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple CPS counter GalaxyCoyote 1 5,876 Jun-09-2019, 07:59 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020