-8

This is my code. I want to return str1 and after 20-30 seconds return str2. I know i cant tipe two times return str1 and below it str2. how can i set timer to it so it returns, i tried like this but dont have ideas any more.

import os
import sys
import time
from weather import *

greeting=(["Change", "change", "CHANGE"]) question_1=(["stop",
"STOP"])

str1 = """Hey [First Name]! 

Are your ready for a free overview of my favorite ways to find new
income opportunities?

This is going to CHANGE the way you look at your options!

It’s kinda weird, but Facebook requires that I confirm you really want
it…so please reply and type  CHANGE"""

str2 = """ Hey [First Name]! Thanks for subscribing! As promised here
is your requested link  https://thebusinessofnursing.com/earn-more-now


P.S. If you ever want to unsubscribe, just type “stop”."""

def classify(msg):
    msg=msg.strip()
    if(msg in greeting):
        return str1

def classify2(msg):
    time.sleep(5)
    return str2

if __name__ == '__main__':
    while(1):
        msg=raw_input("Write Something: ")
        print(classify(msg), classify2(msg))
Nelson
  • 922
  • 1
  • 9
  • 23

2 Answers2

4

You can yield it

def gen():
    yield str1
    time.sleep(sec)
    yield str2

Or

def gen(str_list, delay):
    yield str_list[0]
    for str in str_list[1:]:
        time.sleep(delay)
        yield str  

One of the possible usages could be

for str in gen(str_list, 20):
      do_some_stuff()
taoufik A
  • 1,439
  • 11
  • 20
  • How would i implement it ? Im tring for whole day and im getting more confused :S – Nikola Zivkovic Jan 02 '18 at 23:32
  • 1
    Implement what ? – taoufik A Jan 02 '18 at 23:34
  • the code you wrote, i never used yield. Im really confused now – Nikola Zivkovic Jan 02 '18 at 23:38
  • functions that uses yield are called generators, here is a great explanation https://stackoverflow.com/a/231855/5672176. If you don't care about how generator works and you prefer to call it magic think of it as a return statement that return a value on each iteration (so each time we call the next(my_gen)) – taoufik A Jan 02 '18 at 23:50
1

Easiest way to delay a print

import time

print("first thing")
time.sleep(20)
print("second thing")
JSimonsen
  • 2,642
  • 1
  • 13
  • 13