am trying to write a simple if statement but my IDLE only reads the first part of my code and not the if statement if statement - if-statement

am trying to write a simple if statement but my IDLE only reads the first part of my code and not the if statement
age=int(input("Enter your age"))
if age>=18:
print("Congratulations you are eligible to vote")

Everything is fine, just fix the indentation.
age=int(input("Enter your age"))
if age>=18:
print("Congratulations you are eligible to vote")

Related

How to stop the code execution when using an if statement in AdonisJS?

I'm trying to stop the code if a certain condition is met.
if(condition) {
stop the code
}
This code never gets executed if condition was met.
I tried break;
I tried return;
I tried anything I found on the web.
Adonis always executes the next line of code. Any ideas?
Thanks.

how to get a block of code to run only once in a loop in fortran in a case where the condition repeats

I am running a program in fortran 95. The loop contains a set of conditional actions that reverses under set limits. Please can someone advise me on how to get a block of code to run only once in a loop, such that it does repeat when the condition is met, but runs the rest of the code until the number of iterations is complete?
After reading your question several times, I think I know what you want. If you want a piece of code to always run at least once inside a do loop but possibly repeat, you can try something like this. Start with a logical variable to ensure one execution:
once = .true.
do i = 1, whatever
some code
if (once .or. (another condition)) then
once = .false.
code will always run once but possibly repeat
end if
can have more code
enddo

MySQL Prepared Statements not cleaning up in an infinite loop using C++ Connector

I'm writing a program which updates MySQL tables every few seconds as new data is received. The function has an infinite loop to update constantly, with another loop inside it that iterates through table rows and picks data accordingly.
Unfortunately, no matter what I do, I encounter the most pathetic error:
Can't create more than max_prepared_stmt_count statements
I've tried simply deleting all mysql related values (except con and driver) after each iteration with statements seen in the example code here.
I've tried adding
stmt = NULL;
stmt->close();
Just in case before every value.
I've tried rewriting the program with auto_ptr, which allegedly clean up automatically, based on this answer.
I have even tried closing and reestablishing connection to the MySQL server after each iteration, but even that did not help me.
Please help.
More information on your loop may be required.
But it sounds like your stmt = con->createStatement(); is inside your loop. You only need one connection, before your loop starts.
Then you can execute a statment multiple times in your loop, res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
When your application terminates then you close your stmt, delete stmt;
From mysql.com "The default value is 16,382." mysql.com-sysvar_max_prepared_stmt_count Which it sounds like you only require one.

Unreliable ending of inner loop in python

You know I am a long-time matlab and fortran user. Those languages use explicit end or continue statements to terminate a loop. In Python you must use indentation to identify statements that are in the loop. The example below will not run if I do not put a statement (any statement) at the first indentation level to terminate the inner loop. i.e. if you try to drop down 2 levels of indentation there is an invalid syntax error. So if you comment out the first print statement the error occurs. Is that just the way it is? Is there some kind of dummy continue or enddo statement in Python that can serve as a placeholder for this?
I get this error when using python2.7 on Linux. Note using the Spyder Python 3 environment on PC it runs fine with or without the print statement!
from numpy import *
nt=11
nres=2
t=zeros(nt)
y=zeros((nres,nt))
for it in range(nt):
if it == 0:
t[it]=0.
else:
t[it]=t[it-1]+1./(nt-1)
for ir in range(nres):
y[ir,it]=sin(pi*t[it]+ir*pi/2)
# need a statement at this level to terminate inner for loop
print('it=',it,'t=',t[it],'y(:,it)=',y[:,it])
print('end of outer loop')

Stuck in while loop when writing to file

My code is:
f=file('python3.mp3','wb')
conn=urllib2.urlopen(stream_url)
while True:
f.write(conn.read(1024))
where stream_url is the url of a stream I'm saving to disk in mp3 format (python3.mp3).
The file successfully saves, but the while loop never terminates. I guess I'm a bit confused as to what the 'True' condition represents? Is it while the stream is playing? While the connection is open? I've tried adding conn.close() and f.close() within the while loop, but that throws errors because it seems like it's interrupting the writing process.
while True loops forever, or until you break. You need to break when you've read the whole stream, something like this:
f = file('python3.mp3','wb')
conn = urllib2.urlopen(stream_url)
while True:
packet = conn.read(1024)
if not packet:
break # We've read the whole thing
f.write(packet)
A while loop runs the code in its body for as long as its condition is True. Your condition is always True, so your while loop never ends, and rightly so. The while loop does not care about anything besides its condition.