while-loop

Using while loops to count elements in a list

女生的网名这么多〃 提交于 2019-11-29 18:12:48
places = ["Jack", "John", "Sochi"] count=0 multi_word=0 place = places[count] while place != "Sochi" and count < len(places): if ' ' in place: multi_word += 1 count += 1 place = places[count] print ('Number of cities before Sochi:', count) My code should print the number of cities before Sochi excluding Sochi . I don't understand what this line (place = places[count]) does, nor do I understand why I need it twice. foreach would neaten it up places = ["Jack", "John", "Sochi"] count = 0 for place in places: if ' ' in place: multi_word += 1 if place == "Sochi": break count += 1 count=0 place =

My python code that converts numbers between bases has several errors. What could be wrong and how can I find them?

浪子不回头ぞ 提交于 2019-11-29 18:08:21
My program is a function that converts numbers from one base to another. It takes three arguments: the initial value, the base of the initial value, then the base it is to be converted to. The thing has several errors. For one, the thing won't accept any value that contains a letter for cnum. I don't know why. And I can't seem to figure out how to force the thing to recognize the argument 'cnum' as a string within the function call. I have to convert it into a function in the code itself. Also, I can't get the second half, the part that converts the number to the final base, to work. Either it

Why does program not execute final printf statement?

扶醉桌前 提交于 2019-11-29 18:02:58
I cannot figure out why program control does not reach the third printf, right after the for loop. Why won't the third printf print? If I change the for loop to while loop, it still will not print. Here is the program and output: main() { double nc; printf ("Why does this work, nc = %f\n", nc); for (nc = 0; getchar() != EOF; ++nc) { printf ("%.0f\n", nc); } printf ("Why does this work, nc = %f", nc); } The output is: Why does this work, nc = 0.000000 test 0 1 2 3 4 It works fine for me, how are you trying to termintate the program? The for -loop should end once EOF is detected as input by

How can I stop a While loop?

谁说我不能喝 提交于 2019-11-29 17:34:57
问题 I wrote a while loop in a function, but don't know how to stop it. When it doesn't meet its final condition, the loop just go for ever. How can I stop it? def determine_period(universe_array): period=0 tmp=universe_array while True: tmp=apply_rules(tmp)#aplly_rules is a another function period+=1 if numpy.array_equal(tmp,universe_array) is True: break #i want the loop to stop and return 0 if the #period is bigger than 12 if period>12: #i wrote this line to stop it..but seems it #doesnt work..

Endless loop while using “try and catch ” block inside a “while loop”

帅比萌擦擦* 提交于 2019-11-29 17:11:52
My program has an endless loop, when I use try and catch block in a while loop . import java.util.*; class Try { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(true) { try { System.out.println("Enter a no "); int s=sc.nextInt(); } catch(Exception e) { System.out.println("Invalid input try again"); } } } } When I input an integer, it runs fine and asks for another input, but when I input a char, it goes for endless loop. Why is this so? Your program enters an infinite loop when an invalid input is encountered because nextInt() does not consume invalid tokens.

How to iterate a mysqli result set?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 17:01:07
I want to loop through the result set of the following query: "select uid from userbase" I am currently employing the following loop, but I can get only the first value. $i = 0; $output = mysqli_query($mysqli, "select uid from userbase") or die(mysqli_error($mysqli)); while ($row = $output->fetch_array()) { $deviceToken = $row[$i]; echo $deviceToken; $i++; } What might be the problem? Is it fetch_array() ? You need to define a array and store your data into array inside loop . Use MYSQLI_ASSOC no need for incremented value $deviceToken=array(); while ($row = $output->fetch_array(MYSQLI_ASSOC))

How can I increment a number in a while-loop while preserving leading zeroes (BASH < V4)

℡╲_俬逩灬. 提交于 2019-11-29 16:24:33
I am trying to write a BASH script that downloads some transcripts of a podcast with cURL. All transcript files have a name that only differs by three digits: filename[three-digits].txt from filename001.txt to.... filename440.txt . I store the three digits as a number in a variable and increment the variable in a while loop. How can I increment the number without it losing its leading zeroes? #!/bin/bash clear # [...] code for handling storage episode=001 last=440 secnow_transcript_url="https://www.grc.com/sn/sn-" last_token=".txt" while [ $episode -le $last ]; do curl -X GET $secnow

Progress bar while running while loop

…衆ロ難τιáo~ 提交于 2019-11-29 16:10:12
I have this while loop, that basically loops through a lot of records in a database, and inserts the data in another: $q = $con1->query($users1) or die(print_r($con2->errorInfo(),1)); while($row = $q->fetch(PDO::FETCH_ASSOC)){ $q = $con2->prepare($users2); $q->execute(array($row['id'], $row['username'])) or die(print_r($con2-errorInfo(),1)); } (The script has been shortened for easy reading - the correct one has a much longer array) I would like to do this more graphical, and show a progress bar on how far it has went, instead of just seeing a page loading for a few minutes (there are ~20.000

Access variable inside while loop from outside (C#)?

我是研究僧i 提交于 2019-11-29 16:02:33
I'm New to C# and I'm trying to reach the value of MAX from the while so i can use it outside but i can't...anyone have some ideas !!! Thanks In Advance while (Condition) { Double MAX = somecode..... ..... } Console.WriteLine("The OPTIMAL Value : " + MAX); Declare MAX before you start the while loop. The way you have it you can only access within the while. Double MAX = 0; while (Condition) { MAX = somecode..... ..... } Console.WriteLine("The OPTIMAL Value : " + MAX); You must declare the variable BEFORE the loop. Double MAX; while (Condition) { MAX = somecode.... } Console.WriteLine("The

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

元气小坏坏 提交于 2019-11-29 15:58:42
问题 I tried the following code different ways, like by taking out the while or the if, but when I put both together (if and while), I always get the error at the end... undefine numero set serveroutput on accept numero prompt 'Type # between 100 and 999: ' declare i number:=1; a char(25); b char(1); c varchar2(10); d number; begin c := &numero; d := length(c); b := substr(c, i, 1); while i <= d loop if b = '1' then a:= a||'one '; end if; i := i+1; end loop; dbms_output.put_line('The number is '|