Yyyksjddbdj
Yyyksjddbdj

Reputation: 11

How to count the amount of the printed numbers in an interval?

My code looks like this, it's almost done, just i need to count the amount of those printed numbers, and print that amount out with them.


start = int(input("Input the first number: "))
end = int(input("Input the last number: "))

 
for i in range(start,end):
  if i > 1:
    for j in range(2, i):
        if i % j==0 :
          break
    else:
      print(i)

Output:

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

The question is, that i want to print out that how many numbers are printed out there. Thanks for your answers!

Upvotes: 1

Views: 752

Answers (2)

Michael A
Michael A

Reputation: 4615

Just create an incrementing variable and increment it every time you print something. Then at the end, print that variable.

count = 0

start = int(input("Input the first number: "))
end = int(input("Input the last number: "))
for i in range(start,end):
    if i > 1:
        for j in range(2, i):
            if i % j == 0 :
                break
        else:
            count += 1
            print(i)
            
print(f"Total numbers printed: {count}")

Upvotes: 1

Eoghan1232
Eoghan1232

Reputation: 45

start = int(input("Input the first number: "))
end = int(input("Input the last number: "))
counter = 0  
  for i in range(start,end):
    if i > 1:
      for j in range(2, i):
          if i % j==0 :
            break
      else:
        print(i)
        counter = counter + 1
  print(counter)

If I understand this is what you are looking for? Everytime it prints, at the end you want to know how many times overall? So when it goes to print, use a counter to keep track then print it when the for loop finishes.

Upvotes: 1

Related Questions