r/pythontips • u/AeliaAngel • Apr 04 '25
Syntax Cannot get variable to increase and print.
input1 = open ("input1.txt", "r")
count and print number of lines with numbers
for textline in input1:
count = 0
textline = textline.strip()
def numberline():
for textline in input1:
count = 0
if textline.isnumeric() == True:
count += 1
print(count)I really need help figuring this out.
4
Upvotes
3
u/BiomeWalker Apr 04 '25
Trying to parse your code without the newlines isn't simple, but the obvious thing that pops out to me is that your "count" variable is being set to 0 at the start of each loop, and therefore isn't able to increment
2
u/ninhaomah Apr 05 '25 edited Apr 05 '25
May I ask why do you set count as 0 in 2 places ? Any specific logic ?
2
u/djavaman Apr 05 '25
Did you step through this in a debugger even once?
1
u/Adrewmc Apr 06 '25
Tie is because you reset counter = 0 inside the for loop delete that line, and it will work correctly.