Counting consecutive selections of a given number when sampling integers in a range This script counts consecutive number strikes of a number that you determine in a number-sequence that you also determine.

I would be very pleased if this script can be reviewed in terms of compactness, readability and quality. If this is perfect, please also let me know. But I am shure it will not be.

The script can be tested here

import random

desired_number = int(input("Desired number: "))
lower_range = int(input("Lower range: "))
upper_range = int(input("Upper range: "))
iterations = int(input("Iterations: "))

consecutive_strikes = 0
biggest_strike = 0

for i in range(iterations):
  actual_number = random.randint(lower_range, upper_range)
  print("Actual number: ", actual_number)

  if actual_number == desired_number:
    consecutive_strikes += 1
  else:
    if (consecutive_strikes &gt; biggest_strike):
      biggest_strike = consecutive_strikes
    consecutive_strikes = 0

# if all numbers are the desired number, the second if-statement in the for-loop  
# would never be executed 
if (consecutive_strikes &gt; biggest_strike):
  biggest_strike = consecutive_strikes

print("Biggest consecutive strike: ", biggest_strike)
You can use the itertools for this.

Just groupby the value to get groups of equal values, filter for the desired value, then get the length of each streak and finally take the max of that length:

from itertools import groupby

def longest_streak(values, desired_value):
    return max(len(list(group))
               for value, group in groupby(values)
               if value == desired_value)


(I think it should be streak, 4. b: "an uninterrupted series" and not strike.)

Then your main can code become this:

from random import randint

if __name__ == "__main__":
    desired_number = int(input("Desired number: "))
    lower_range = int(input("Lower range: "))
    upper_range = int(input("Upper range: "))
    iterations = int(input("Iterations: "))

    numbers = (randint(lower_range, upper_range) for _ in range(iterations))
    print("Biggest consecutive strike: ", longest_streak(numbers, desired_number))


Here numbers is a generator, so the function can just consume the numbers as they are generated (meaning that this will occupy at most len(group) space in memory). 

if __name__ == "__main__": is a guard so that the code under it is only executed when directly executing this script, but not when importing from it.