Minute
Minute

Reputation: 13

How to Print Something Every Other Time Something Happens

I'm trying to learn python and while learning I've come across a bit of a problem.

import time
import pyautogui

def SendScript():
    time.sleep(2)
    with open('script.txt') as f:
        lines = f.readlines()
    for line in lines:
        time.sleep(2)
        pyautogui.typewrite(line.strip())
        pyautogui.press('enter')
SendScript()

I'm trying to print something to the screen every second time the 'enter' key has been pressed, but I'm an extreme beginner so I really don't know how to do that. Could someone help me accomplish this task?

Upvotes: 0

Views: 510

Answers (4)

tripleee
tripleee

Reputation: 189908

As indicated in another answer already, a simple toggle can be implemented with a bool and then code which toggles it every time something happens:

thing = False
:
if happens(something):
    thing = not thing

This is fine for toggling between two states. A more general approach which allows for more states is to use a numeric variable and a modulo operator:

times = 0
maxtimes = 12
:
if happens(something):
    times += 1
    if times % maxtimes == 1:
        print("ding dong")

The modulo could be compared to 0 instead if you want to print on the 12th, 24th etc iterations instead of the first, the 13th, etc, or of course any other offset within the period if that's what you want.

Another useful trick is to flip-flop between zero and some other value.

value = 0
othervalue = 1234
:
if happens(something):
    value = othervalue - value

Of course, you can flip-flop between any two values actually; subtract the current value from their sum to get the other one.

Needless to say, just toggling or flip-flopping isn't very useful on its own; you'd probably add some (directly or indirectly) user-visible actions inside the if happens(something): block too.

Upvotes: 1

salilnaik
salilnaik

Reputation: 46

You could create a new boolean variable to track if the enter key has been pressed before. That way, every time the for loop iterates, the value of pressed switches and only when the value of pressed is True will it print something.

import time
import pyautogui

def SendScript():
    pressed = False
    time.sleep(2)
    with open('script.txt') as f:
        lines = f.readlines()
    for line in lines:
        time.sleep(2)
        if pressed:
            print("Something")
        pressed = not pressed
        pyautogui.typewrite(line.strip())
        pyautogui.press('enter')
SendScript()

Upvotes: 1

lucidbrot
lucidbrot

Reputation: 6251

You could use a generator for this:

def everySecondTime():
    while True:
        yield "hi"
        yield "not hi"

mygen = everySecondTime()
print(next(mygen))
print(next(mygen))
print(next(mygen))
print(next(mygen))

This prints

hi
not hi
hi
not hi

I'm sure it's clear to you how you could adapt this to do some other actions instead.

Whether this approach is better than just using a boolean is highly debatable, but I thought I'd leave it here so you could learn about generators (the yield keyword) if you want to.

Upvotes: 0

zabop
zabop

Reputation: 7922

From a more step-back approach, you could do:

events=['event1', 'event2', 'event3', 'event4', 'event5', 'event6', 'event7', 'event8']

counter = 0
for event in events:
    counter += 1
    if counter % 2 == 0: # ie do stuff when divisible by 2, ie when its even
        print('print what you want to be printed every second time')
    else:
        pass

Of course you are not looping through events like I do in this example. The point is counting the events and only doing stuff when this count is even.

Upvotes: 1

Related Questions