Find How Many Times Numbers Repeat on a Digital Clock Project Overview:

See how many times numbers would repeat x times on a digital clock output. This particular code only counts from 12:00 to 11:59, so it's technically only half of a day, I could take the (output x 2) for a full day.

Functions:

input_io():
Input the number of times you would like to see if a number repeats (for example 11:11 will have the number "1" repeated four times. 12:11 would have 3 repeated "1"s, etc.) 

gen_times():
Generate a list of all the "digital clock" outputs starting from 12:00 to 11:59 - including leading zeros for minutes (e.g. 12:04 would need the zero before the "4").

count_repeat():
Test each "clock output" (from gen_times()) to see how many times the numbers 0-9 repeat in each one. If it is greater than the input time add one to the counter.

Questions:


In the code, you'll see that I return values from functions and immediately assign them to variables. I'm not sure the proper conventions of this in Python. Are there ways to pass-through variables from function to function - is it reasonable to assign variables outside of functions to function outputs?
Is there a better way to run this program without all of the coercion? The main use case of the coercion is for the leading zeros when generating times.


Code:

def input_io():
    input_repeats = input("Please input the amount of times you'd like to see if numbers repeat")

    while True:
        try:
            input_repeats = int(input_repeats)
            break
        except:
            print("Please enter an integer.")
            input_repeats = input()
    return input_repeats

input_repeats = input_io()


def gen_times():

    total_minutes = (24 * 60) / 2
    print(f"Total Minutes {total_minutes}")
    times = ["0"] * int(total_minutes)
    hours = "12"
    minutes = "00"
    i = 0

    while (i &lt; total_minutes):

        if(hours==13):
            hours=1


        times[i] = str(hours)+str(minutes)
        #print(int(minutes))



        if(minutes == "59"):
            minutes = "00"
            hours = int(hours) + 1

            #ADDING "0" IN FRONT OF HOURS
            #if(int(hours) &gt;= 10):
            #    hours = hours
            #else:
            #    hours = "0"+str(hours)

        else:
            minutes = str(int(minutes)+1)

            if (int(minutes) &gt;= 10):
                minutes = minutes
            else:
                minutes = "0" + str(minutes)

        i += 1
    return times

times = gen_times()
print(times)


def count_repeat():
    counter_repeat = 0
    i=0
    ii=0
    iterationcounter = 0

    while(i != len(times)):

        ii=0

        while(ii &lt; 10):
            count = times[i].count(str(ii))
            #iterationcounter+=1
            if count &gt;= int(input_repeats):
                counter_repeat+=1
                break

            ii+=1
        i+=1
    return counter_repeat

counter_repeat = count_repeat()


print(f"Number of Times w/ {input_repeats} repeats: {counter_repeat}")
print(counter_repeat,"/",len(times))
percent_repeat = (counter_repeat/len(times))
print("{:.2%}".format(percent_repeat))
#print(iterationcounter)
Input loop

Your

        print("Please enter an integer.")


can probably be lumped into the input statement after it.

Time math

Usually doing your own should be avoided. As an example, this:

total_minutes = (24 * 60) / 2


can be

from datetime import timedelta
# ...
total_minutes = timedelta(hours=12) / timedelta(minutes=1)


General approach

Currently you're doing a lot of string manipulation, including a stringly-typed hours and minutes in gen_times. Your code would be simpler if, instead of maintaining and incrementing those strings, increment a single datetime.time instance. From there, to get individual digits, you could  strftime it to a string and feed that to a Counter. The strftime format string in this case would be most useful as '%I%M'.

Variable names

Try to avoid names like i and ii. In particular, this:

    while(ii &lt; 10):
        count = times[i].count(str(ii))
        #iterationcounter+=1
        if count &gt;= int(input_repeats):
            counter_repeat+=1
            break

        ii+=1


is better represented as

for digit in range(10):
    count = times[i].count(str(digit))
    if count &gt;= int(input_repeats):
        counter_repeat+=1
        break


Though that times[i].count should be replaced with the use of a Counter.