I need help here I wrote code for my lab that "The loop will iterate a total of 10 times using the value of a variable that is incremented by one each time the loop completes."strong text but when I run my python file by click just open it refuse to run in command prompt...When I test in Edit with IDLE it runs fine...I can't figure out why it wont run...
here is what I wrote for my code:
counter = 0
while counter < 10:
counter = counter + 1
print counter
print "the loop is finished"
Perhaps it's just done way too fast.
Have you tried adding a raw_input() at the bottom of the file?
It looks like you have indentation errors (mainly with the print statements being indented one space further than the code above them).
Does this work for you:
counter = 0
while counter < 10:
counter = counter + 1
print counter
print "the loop is finished"
Python has strict indentation rules, if you want to learn more about them I would suggest reading this wikipedia page, it's pretty good.
Related
I have a loop that executes a large number of times (say 25000). Inside the loop there is a code to add a row to a DataGridView.
dvg->Rows->Add();
Complete code below:
for(int i = 0 ; i < m_Items->Length ; i++)
{
dgv->Rows->Add();
dgv->Rows[i]->Cells[this->IDColumn->Index]->Value = safe_cast<System::Object^>(0);
dgv->Rows[i]->Cells[this->ModColumn->Index]->Value = <<"Data from database">>
dgv->Rows[i]->Cells[this->RunColumn->Index]->Value = <<"Data from database">>
}
This statement is taking a couple of seconds to execute 25000 times. So I am trying to optimize this code for time.
I know the number of rows prior to entering the loop. So I thought moving this call out of the loop and call a function that will add 25000 rows at once. I searched online and this is what I tried so far.
I tried using DataGridView::AddRange function by passing a DataGridViewRow object as an argument. But the sample code found online is confusing. Since I am new to C# code, I don't know how to proceed.
I declared
DataGridViewRow Test;
Now, how to write the code that tells, the columns in DataGridView need to be same as the columns in dvg (If that is even required)?
Do I have to fill the data to test before calling DataGridView::AddRange?
Can somebody help, please?
I am using a chrono to get the time elapsed during a loop. I would like to display this time every loop run.
I can do this:
for(int i = 0;i<3;i++)
{
sleep(2 secs);
time= get_time();
cout<<"time is : "<<time;
}
But I have the output:
time is : 2 time is : 4 time is : 6
I could add an endl to have it in column but that is not what I want. My loop is about million times, so I don't really want to print a million lines.
I would just like to print like:
time is : 2
and then refresh it to
time is : 4
and so on. Is there a way to do this?
You can use endl with clrscr() .
printing to terminals is very easy, but it can be extremely hard at the same time. At its core, a terminal is simply a file, that you can use to write or read on. Performing tasks such as changing cursor's position is in fact system-specific and your code will have to be platform dependent.
But don't panic! People have done it before and even wrote libraries to do it. I think NCurses will do the job. https://www.gnu.org/software/ncurses/ncurses.html
I advice you to refer to this thread to see some issue related to your question: How to clear a specific line with NCurses?
I have never used ncurses my self, so I wish you best of luck!
Enjoy programming!
I have been given a homework but don't know how to write some part of it explained as following:
..."In addition make sure your program keeps running until the user enters “quit”. To keep your program running you should use “for” statements. However, for statements doesn’t provide your program to run to infinity. Therefore, for your range value, give the maximum value that your OS can handle. For example; if you are using a 32 bit OS and interpreter, your computer should use 2^31 as the maximum value. "
What did our teacher mean do you think?(PYTHON 2.7)
You have to make mention about the language.
Java program:
do {
//Show information
//Execute preliminar code
//Here you can get the Exit command
} while (Exit command is true);
Hope this helps!
Maybe you could write a code that consistently asks the user for an input. For example:
number = int(input("Enter integer less than 5: "))
while number>5:
print("Try again")
number = int(input("enter integer number"))
if number == quit:
break
If I did this correctly, this program will continue until one of two conditions is met, 1) you enter a number less than five OR 2) you enter the word quit
As I learned later,we were supposed to use
for i in range(2**31):
#Your program codes apart from function definitions are here.
.
string=raw_input("Enter quit to exit.")
if (string == "quit"):
break
I am using gdb-7.0.1 and I think I have detected a bug in a certain section of my code, which
has a for loop. The for loop looks like
for (int i=0 ; i< end ; ++i )
{
//Code here.
}
Here end is a very large integer. The code does not crash at the first iteration, and seems to crash somewhere at iteration number end/2.
Since I want to understand the behaviour of the code at iteration number end/2 , just stepping and nexting from i=0 till I reach this iteration point, is unfeasible.
Is there a way to tell gdb to continue through a for loop till i gets the value end/2 and then wait for the user to manually step through iteration number end/2?
I am using gcc-4.5.2 on Ubuntu Linux
Here's a tutorial on conditional breakpoints with gdb.
I'm guessing you didn't know the term for this, otherwise it would have been easy to google.
When you set the breakpoint it'll give you a breakpoint number (for the moment, let's assume it's 1). You'll then make that breakpoint conditional, something like:
condition 1 i==end/2
You have to use conditional breakpoint. Here is more about it: http://www.cs.cmu.edu/~gilpin/tutorial/#3.4
And on SO: How do I set a conditional breakpoint in gdb, when char* x points to a string whose value equals "hello"?
In your case (not tested):
break <line_number> if i==end/2
You should be able to place an if (i == (end/2 -1)) { Foo; } in there then set a breakpoint at Foo, which would allow you to continue stepping from there.
If end is big (in the tens of thousands), then the conditional breakpoint solution can be very slow - gdb has to evaluate the condition each time round the loop. If this is a problem for you, then you can use this trick:
for (int i=0 ; i< end ; ++i )
{
if (i == end/2)
i %= end ; // This has no effect, but lets you set a breakpoint here
//Code here.
}
I do this all the time :-)
Another solution is to set a skip-count on the breakpoint. I use gdb in a Qt environment, so I can't give you the gdb syntax. But it's faster than setting a condition.
I am trying to find an error in my code. The problem is the error occurs in a loop. But the loop iterates about 500 times. Instead of clicking through the loop. Is it possible to skip over a certain amount of the loop ??
VS allows you to set a condition on a breakpoint in terms of variables that are in scope. So, in your case, you can test against the loop counter.
Here is a crude answer:
if ((iter % 10) == 0) {
int stop = 1;
}
Then place a break-point at "int stop = 1;". Perhaps, there is a better way in VS but this is what I do from time-to-time.
You can assign new values to variables during debug session. Step through the loop statements as many times as you like, then set your loop counter (or whatever other vars maintain loop condition) to terminate the loop.
Just put the breakpoint in the loop like indicated below >>. Use F5 to get to the condition that causes failure so you can loop through the individual pass. How to know where to break is up to you.
for (int i = 0; i < LOOPMAX; i++) {
>>some_proc(i);
some_other_proc(i);
some_third_proc(i);
}
By pressing F5 it'll continue running till it gets to the next breakpoint (the next pass through the code). Sure you'll have to hit it 500 times, but that beats some thousands of times. Combine this with #Troubador code above.
PS: This answer IS really simple, but some people don't know they can do this.