while-loop

Cannot break out of while loop in python

孤者浪人 提交于 2020-01-04 17:33:14
问题 Cannot break out of while loop in python: I tried merging the code all together with no main and getheight functions, and it still gives me an infinite loop. def main(): x = 0 while x not in range (1,23): getheight() if x in range (1,23): break for i in range (x): for j in range (x - j): print (" ", end="") for j in range (x): print ("#", end="") print (" ", end="") for j in range (x): print ("#", end="") "\n" def getheight(): x = input("Give me a positive integer no more than 23 \n") return

Output sum of even numbers between two integers

删除回忆录丶 提交于 2020-01-04 06:52:08
问题 I am working on a simple JAVA question in one of my college courses. I am stumped on this one program. I will display what I have so far and give the question I have to answer. I also looked at a similar question on StackOverflow, BUT it isn't the same problem so it DIDN'T help. The program I need to write is: Write a program that uses 'while' loops to perform the following steps: a.) Prompt the user to input two integers: 'firstNum' and 'secondNum' (firstNum must be less than secondNum) b.)

PHP - remove comma from the last loop

旧城冷巷雨未停 提交于 2020-01-04 05:55:07
问题 I have a PHP while LOOP, and I want to remove last comma , from echo '],'; if it is last loop while($ltr = mysql_fetch_array($lt)){ echo '['; echo $ltr['days']. ' ,'. $ltr['name']; echo '],'; } 回答1: $str = ''; while($ltr = mysql_fetch_array($lt)){ $str .= '['; $str .= $ltr['days']. ' ,'. $ltr['name']; $str .= '],'; } echo rtrim($str, ","); this will remove the last , from string 回答2: Create an array with the elements as you go along so that they look like array = ([ELEMENT INFO], [ELEMENT

bash while loop read only one line

霸气de小男生 提交于 2020-01-04 05:36:31
问题 I have issue with this code. while IFS='' read -r line do echo "host: "$line IP_addr=$(echo $line |cut -d" " -f1) host=$(echo $line | cut -d" " -f2) FILE_CHECK=$(ssh -o ConnectTimeout=10 $USER@$IP_addr "find $SRC_FILE -mtime 0") echo $FILE_CHECK ((host_num+=1)) echo "" #empty line done < "$SERVER_LIST" echo $host_num Where variable server list contain path to file with server 192.168.1.1 app1 192.168.1.2 app2 192.168.1.3 app3 Script read first line, successfully connect to host and then exit.

Can you break a while loop from outside the loop?

我与影子孤独终老i 提交于 2020-01-04 05:35:14
问题 Can you break a while loop from outside the loop? Here's a (very simple) example of what I'm trying to do: I want to ask for continuous inside a While loop, but when the input is 'exit', I want the while loop to break! active = True def inputHandler(value): if value == 'exit': active = False while active is True: userInput = input("Input here: ") inputHandler(userInput) 回答1: In your case, in inputHandler , you are creating a new variable called active and storing False in it. This will not

Bash shell script Nested while loop with IFS

孤街浪徒 提交于 2020-01-04 05:16:28
问题 I'm trying to parse a set of csv files using bash shell script the files looks as below: File1: /tmp/test.txt key1,key1file.txt key2,key2file.txt key3,key3file.txt Files: /tmp/inter/key1file.txt abc,cdf,123,456 Files: /tmp/inter/key2file.txt abc,cdf,123,456 Files: /tmp/inter/key3file.txt abc,cdf,123,456 I've tried parsing these files using 2 while loops: while IFS="," read keycol keyfile do while IFS="," read keyval do echo "inside inner while loop" echo "$keycol|$keyval" done < "/tmp/inter/

How to stop infinite loop in JShell / Kulla?

女生的网名这么多〃 提交于 2020-01-04 04:22:07
问题 JShell is a Java REPL that is scheduled to be released alongside Java 9; however, there is an open beta for it. If I create an infinite loop in JShell (Project Kulla) by typing: -> while(true) {} JShell will loop forever. Short of completely quitting JShell, is there a way to stop an individual line of code while it's running in JShell (after you have already begun evaluation of the code)? 回答1: Actually there is a way. Just hit CTRL + c : -> while(true) {} Killed. -> It'll kill the loop and

while loop terminating early with nested if statement

我与影子孤独终老i 提交于 2020-01-03 20:05:58
问题 I'm writing a condition check for a simple input via prompt() in JavaScript. The idea is to repeat a prompt until the user supplies a positive integer. In particular, if I submit a negative integer, it correctly prompts me again via case 3. However, if I answer the second prompt again with a negative integer, the code unexpectedly leaves the while loop. In the if statement: Case 1: checks if the user 'cancels' the prompt box (or returns an empty string), in which case I set a variable

JOINS vs. while statements

大憨熊 提交于 2020-01-03 09:56:43
问题 In the company where I came to work, they run a PHP/MySQL relational database. I had always thought that if I needed to pull different info from different tables, that I could just do a simple join to pull in the data such as.... SELECT table_1.id, table_2.id FROM table_1 LEFT JOIN table_2 ON table_1.sub_id = table_2.id When I got to where I currently work, this is what they do. <?php $query = mysql_query("SELECT sub_id FROM table_1"); while($rs = mysql_fetch_assoc($query)) { $query_2 = mysql

mysql select with while loop

社会主义新天地 提交于 2020-01-03 06:37:12
问题 I would like to select data from my table and using one of the columns, create a loop to define additional data. For example: 'select id,related_id,name from ancestors' id, related_id, name 1, 0, Bob 2, 1, Dave 3, 2, Susie 4, 1, Luke 5, 0, Cindy 6, 5, Sam Bob is the grandfather, Dave and Luke are his children and Susie is his granddaughter. Cindy has a child Sam. Now, I want to use related_id to figure out how many levels the ancestor tree goes down. So I want the results to be: id, related