python - I'm having trouble stopping a loop after N number of times this loop has been executed - Stack Overflow

admin2025-04-17  4

I wanted to make an application that enters code from a file into the game, but I need to specify how many times new codes will be entered and I encountered the problem TypeError: 'int' object is not iterable

import keyboard
import tkinter as tkinter
from tkinter import messagebox, Entry
import tkinter as tk
from time import sleep
import pyautogui
isClicking = False
lim = 0
window = tk.Tk()
window.title("Auto Code")
window.geometry('720x360')
file = open('promo.txt', 'r')
def ser():
    global lim
    lim = text.get()
    if lim != '':
        text.delete(0, 'end')
    else:
        tk.messagebox.showinfo("Error", ('Text Empty'))
def tumb():
    global isClicking
    if isClicking:
        isClicking = False
        print('Off')
    else:
        isClicking = True
        print('On')
    if isClicking:
        for i in file and range(int(lim)):
            pyautogui.moveTo(1564, 1008)
            pyautogui.press('F6')
            sleep(0.3)
            pyautogui.press('F6')
            pyautogui.write(i)
            sleep(0.5)
            pyautogui.moveTo(838, 584)
            pyautogui.press('F6')
            sleep(0.2)
            pyautogui.press('F6')
            sleep(2)
            pyautogui.click(977, 733)
            pyautogui.press('F6')
            sleep(0.2)
            pyautogui.press('F6')
            sleep(1.5)

text = tk.Entry(fg = "Black", width = 10)
text.place(x = 220, y = 65)
btn = tk.Button(window, text = "Add", command = ser, width = "40", height = "2", fg = "black", bg = "white")
btn.place(x = 220, y =110)
keyboard.add_hotkey('ctrl + v', tumb)
window.mainloop()
file.close()

I tried to redo the loop but it continues to write text from the file

I wanted to make an application that enters code from a file into the game, but I need to specify how many times new codes will be entered and I encountered the problem TypeError: 'int' object is not iterable

import keyboard
import tkinter as tkinter
from tkinter import messagebox, Entry
import tkinter as tk
from time import sleep
import pyautogui
isClicking = False
lim = 0
window = tk.Tk()
window.title("Auto Code")
window.geometry('720x360')
file = open('promo.txt', 'r')
def ser():
    global lim
    lim = text.get()
    if lim != '':
        text.delete(0, 'end')
    else:
        tk.messagebox.showinfo("Error", ('Text Empty'))
def tumb():
    global isClicking
    if isClicking:
        isClicking = False
        print('Off')
    else:
        isClicking = True
        print('On')
    if isClicking:
        for i in file and range(int(lim)):
            pyautogui.moveTo(1564, 1008)
            pyautogui.press('F6')
            sleep(0.3)
            pyautogui.press('F6')
            pyautogui.write(i)
            sleep(0.5)
            pyautogui.moveTo(838, 584)
            pyautogui.press('F6')
            sleep(0.2)
            pyautogui.press('F6')
            sleep(2)
            pyautogui.click(977, 733)
            pyautogui.press('F6')
            sleep(0.2)
            pyautogui.press('F6')
            sleep(1.5)

text = tk.Entry(fg = "Black", width = 10)
text.place(x = 220, y = 65)
btn = tk.Button(window, text = "Add", command = ser, width = "40", height = "2", fg = "black", bg = "white")
btn.place(x = 220, y =110)
keyboard.add_hotkey('ctrl + v', tumb)
window.mainloop()
file.close()

I tried to redo the loop but it continues to write text from the file

Share Improve this question edited Jan 30 at 18:29 John Kugelman 363k69 gold badges553 silver badges597 bronze badges asked Jan 30 at 17:54 RaliosRalios 11 silver badge1 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 1

This does not work the way you expect:

for i in file and range(int(lim))

You can't combine a for loop and a range comparison like that.

I think this is what you want:

for i in file:
    if int(i) >= lim:
        break

You can use zip() to loop through the file and range in parallel. When you zip two sequences of different length, it will stop when it reaches the end of the shorter one, so this will stop when it reaches the end of the range.

for i, _ in zip(file, range(int(lim)):

But a more common way to code this is to just use a counter variable and break out of the loop when you reach the limit.

line_count = 0
for i in file:
    line_count += 1
    if line_count > lim:
        break

    # rest of loop code

Or use enumerate():

for counter, i in enumerate(file):
    if counter > lim:
        break
    # rest of loop

for i in file and range(int(lim)): does not properly iterate over both the file and the number of times you want the loop to run. file and range(int(lim)) evaluates to range(int(lim)) if file is truthy, which means the loop only runs over range(int(lim)) instead of the file content.

When you do for i in file, it reads each line from the file. Once the file is read, its cursor is at the end, so repeated iterations may not work as expected unless you reopen or reset it.

Solution

  1. You can convert the input to an int

lim = int(text.get())

  1. Iterate over the file correctly
with open('promo.txt', 'r') as file:  
   for _ in range(lim):  # Execute the loop `lim` times
           line = file.readline().strip()  # Read a single line
                if not line:
                 print("End of file reached.")  
                      break  
转载请注明原文地址:http://anycun.com/QandA/1744900619a89213.html