0

I intend to send message to many numbers, but this is my code and issue (code is made shorter and required lines are here):

msg = 'test message'
phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']
hours = range(0, 25)
minutes = range(0, 60)
for phone_number in phone_numbers:
    for hour in hours:
        for minute in minutes:
            sndmsg(phone_number, msg, hour, minute)

I know my way is incorrect, because this is the output but I'm not sure how to solve this. Googling this did not help me.

Output:

test message to +989111111111 will be sent on 0 0
test message to +989111111111 will be sent on 0 1
test message to +989111111111 will be sent on 0 2
...
test message to +989111111112 will be sent on 0 0
test message to +989111111112 will be sent on 0 1
test message to +989111111112 will be sent on 0 2

My desired output would be like:

test message to +989111111111 will be sent on 0 0
test message to +989111111112 will be sent on 0 1
test message to +989111111113 will be sent on 0 3

I want to send the message to each number in each minute like the above output, how may I reach this?

2
  • 2
    In your example, you have much more minutes than phone numbers. So you want to cycle the phone numbers to exhaust the send times, or do you want to stop after each number has been sent a message? Commented Feb 12, 2021 at 9:39
  • I want to stop each number after message is sent, and each message should be within one minute, so no two messages are sent in the same minute. in 0 0 the first phone is sent, then we have nothing to do with that number anymore. Again, the second number in 0 1, and so on. Commented Feb 12, 2021 at 9:43

4 Answers 4

3

Try using a generator for the hours and minutes and zip with the phone number:

msg = 'test message'
phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']

def gen_hour_min():
    hours = range(0, 25)
    for hour in hours:
        minutes = range(0, 60)
        for minute in minutes:
            yield hour, minute

for phone_number, hour_min in zip(phone_numbers, gen_hour_min()):
    hour, minute = hour_min
    print(phone_number, hour, minute)
Sign up to request clarification or add additional context in comments.

1 Comment

Note: if your numbers far exceed the amount generated by gen_hour_min() try using zip(phone_numbers, itertools.cycle(gen_hour_min())). In fact gen_hour_min() can be replaced by itertools.product(range(25), range(60))
1

You can create an iterator and then break out when all iterated

phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']

def schedule(numbers):
    iterator = iter(numbers)
    for hour in hours:
        for minute in minutes:
            curr_number = next(iterator)
            if curr_number is None:
                return
            sndmsg(curr_number, msg, hour, minute)
            
schedule(phone_numbers)

Additionally, you could just set minutes as an integer and use math to iterate

def schedule(numbers):
    minutes = 0
    for number in numbers:
         sndmsg(curr_number, msg, minutes // 60, minutes % 60)
         minutes += 1

Comments

0

you dont need to iterating on the time

you can increase the time after you send a message

msg = 'test message'
phone_numbers = ['+989111111111', '+989111111112', '+989111111113', '+989111111114']
hour =0
minute =0
for phone_number in phone_numbers:
   sndmsg(phone_number, msg, hour, minute)
   hour=hour+1 if minute==60 else  hour
   minute =minute+1 if minute <60 else 0

Comments

0

Because you are iterating over the phone_number loop, phone_number loop should be the inner loop:

minute = 0
outer: for hour in hours:
    for phone_number in phone_numbers:
        if minute > 60:
            minute = 0
            continue outer
        sndmsg(phone_number, msg, hour, minute)
        minute = minute+1                
        break outer

2 Comments

This will still send a phone number multiple messages, just in a different order
yes you are right. Sorry, Should have added a break statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.