Home > OS >  How to keep track of console output lines in Python?
How to keep track of console output lines in Python?

Time:10-22

I'm trying to find the number of numbers divisble by 2 in 0-100, I have used a range() function and n1=n1 1 to generate 0 to 100, then with an if statement display only numbers wholely divisble by 2.

For the life of me I cannot find a way to count the number of times 0's are printed in the console, I googled count() but that seems to work for opening other files not the one you are in.

n1 = 1
for x in range(100):
  dTwo = n1 % 2
  if (int(dTwo) == 0):
    print(n1)
  n1 = n1   1  #generates 0 to 101
#how to I keep track of the number of 0s outputted in console after this step?

Any help would be greatly appreciated

CodePudding user response:

count = 0
v = list()
for i in range(1, 101):
    if i % 2 == 0:
        print(i)
        count  = 1
        v.append(i)
print(f'Number of values divisible by 2 = {count}')
print(f'Values that are divisible by 2: {v}')

CodePudding user response:

As much as i get, i think you want to count number of zeros? If i am right then this is the code

count=0
while True:
    y=input("Enter again : Y/N ")
    if y=='Y':
        n=int(input("Enter numbers as you wish :"))
        if n==0:
            count=count 1
    elif y=='N':
        break
print("Total 0's : ", count)

If you want something else then mentioned me before downvote. I really appreciate it.

  • Related