Printing a Statement Only Once in a While Loop

Printing a statement only once in a while loop

It can be done very easily.

boolean isItPrinted = false;

while (lettersSelected == false) {

if ((finalNum.size() == 6) && (isItPrinted == false)) {
System.out.println("3. Press 3 to generate target number!");
isItPrinted = true;
}

How to print only once inside a while loop in python

Use a counter that will input values continuously and print only once.

i=1
while True:

# Asking for input(a and b)
a = int(input('enter a number'))
b = int(input('enter another number'))
if i==1:
i=i+1
if a>b:
print('a is bigger then b')
else:
print('b is bigger than a')
else:
continue

How do I only print statement once in a while loop?

I guess what you want to do is to display this error message only once you have read the entire file. You could use a boolean variable stationFound initialized to false and that you set to true when you found a matching station.
After your while loop, you can then check if stationFound is true, and if not display your error message.

Note that you can also use a break once you have found a matching station to avoid reading the whole file when not needed.

Here is what it would look like in code:

boolean stationFound = false;
while(inputFile.hasNextLine())
{
// ...
if(stationName.equalsIgnoreCase(name))
{
System.out.println("Station code: " + code + " name: " + name);
System.out.println("distance " + distance + " kms, is on the " + line + " line");
stationFound = true;
break;
}
}


if(!stationFound)
{
System.out.println("No information was found for station " + stationName);
}

Print statement only once in a while loop (Python, Selenium)

You just need to change the order of your code, and you don't need an if..else statement. Let it default to "Checking availability...", and then start your loop which will refresh the page every 45 seconds until that text you specified is gone from driver.page_source

driver.get("Link of product site.")
print("Checking availability...")
while "Not available right now." in driver.page_source:
sleep(45)
driver.refresh()


print("It is available! Gonna order it now..")

If it is available immediately it will just print these both out right after each other..

Checking availability...
It is available! Gonna order it now..

Otherwise there will be a time delay between them.

If you want to add a timestamp so you know how long it took to become available use this:

import datetime

current_time = datetime.datetime.now()

driver.get("Link of product site.")
print("Checking availability...")
print(current_time)
while "Not available right now." in driver.page_source:
sleep(45)
driver.refresh()


print("It is available! Gonna order it now..")
print(current_time)

How to print out a statement only once in loop

Try to "remember" if n was found. If not print "not found" once after the loop is done:

line = input()
n = input()
line = line.split()
pos_list = []
x = 0
found = False
for j in range(len(line)):
pos_list.append(j)
#print(pos_list)
for i in line:
if i == n:
print(line.index(n, x))
found = True
x = x + 1
if not found:
print("not found")

How to print a message only once in a loop

Put the conditional error message print after your for loop. Leave the cout for displaying the array number inside the for loop so it is output for every iteration of the loop.

for (int i = 0; i<MAX_ROWS;i++) {
for (int j = 0; j<MAX_COLUMNS; j++) {
inFile >> ArrB[i][j];
if (ArrB[i][j] == -1) {
bad = true;
cout << "The array does not have enough integers" << endl;
break;
}
else {
if (ArrB[i][j] < 1) {
invalidnum = true;
}
}

cout << * (*(ArrB + i) + j) << " ";
}
}

if (invalidnum = true) {
cout << "There is/are negative number(s) or zero(s) in the array imported from your text file." << endl;
}

How do I run a conditional statement "only once" and every time it changes?

It's really not clear what you mean, but if you only want to print a notification when the result changes, add another variable to rembember the previous result.

def shortIndicator():
return indicate_5min.value5 and indicate_10min.value10 and indicate_15min.value15

previous = None
while True:
indicator = shortIndicator()
if previous is None or indicator != previous:
if indicator:
print("Trade possible!")
else:
print("Trade NOT possible!")
previous = indicator
# take a break so as not to query too often
time.sleep(60)

Initializing provious to None creates a third state which is only true the first time the while loop executes; by definition, the result cannot be identical to the previous result because there isn't really a previous result the first time.

Perhaps also notice the boolean shorthand inside the function, which is simpler and more idiomatic than converting each value to an int and checking their sum.

I'm guessing the time.sleep is what you were looking for to reduce the load of running this code repeatedly, though that part of the question remains really unclear.

Finally, check the spelling of possible.



Related Topics



Leave a reply



Submit