标签: while-loop

如何停止While循环?

我写了while loop一个函数,但不知道如何阻止它.当它不满足其最终条件时,循环就永远不会发生.我怎么能阻止它?

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....help..
            return 0
        else:   
            return period
Run Code Online (Sandbox Code Playgroud)

python while-loop

12
推荐指数
2
解决办法
16万
查看次数

使用循环将对象添加到列表(python)

我正在尝试使用while循环将对象添加到列表中.

这基本上就是我想做的事情:

class x:
     pass

choice = raw_input(pick what you want to do)

while(choice!=0):
    if(choice==1):
       Enter in info for the class:
       append object to list (A)
    if(choice==2):
       print out length of list(A)
    if(choice==0):
       break
    ((((other options))))
Run Code Online (Sandbox Code Playgroud)

我可以将对象添加到列表中,但我仍然坚持如何在循环中向列表中添加多个对象.

这是我到目前为止的代码:

print "Welcome to the Student Management Program"

class Student:  
    def __init__ (self, name, age, gender, favclass):  
         self.name   = name  
         self.age    = age  
         self.gender = gender  
         self.fac = favclass  

choice = int(raw_input("Make a Choice: " ))

while (choice !=0):
    if (choice==1):  
        print("STUDENT")  
        namer …
Run Code Online (Sandbox Code Playgroud)

python loops list object while-loop

12
推荐指数
1
解决办法
10万
查看次数

在while循环中使用scanf

这个非常简单的问题可能是一个非常简单的答案:

我正在阅读Pratta的"C Primer Plus",他一直在使用这个例子

while (scanf("%d", &num) == 1)...
Run Code Online (Sandbox Code Playgroud)

== 1真的有必要吗?似乎可以写一下:

while (scanf("%d", &num))
Run Code Online (Sandbox Code Playgroud)

似乎相等测试是不必要的,因为scanf返回读取的对象数,1将使while循环成为真.是确保读取的元素数量正好为1还是完全多余的原因?

c equality scanf while-loop

12
推荐指数
2
解决办法
4万
查看次数

在android中实现while循环

我无法理解android中while循环的实现.

每当我在onCreate()bundle中实现while循环时,(代码如下所示)

public void onCreate(Bundle icicle) {       
  super.onCreate(icicle);
  setContentView(R.layout.main);
  TextView=(TextView)findViewById(R.id.TextView);
  while (testByte == 0)
      updateAuto(); 
}
Run Code Online (Sandbox Code Playgroud)

什么都没有启动,程序在一段时间后进入"悬挂"状态,我无法理解为什么.Testbyte如下:

byte testByte == 0;
Run Code Online (Sandbox Code Playgroud)

并且updateAuto()应该每1秒更新一次代码并显示在textView部分内.我一直在updateAuto()里面使用setText,如下所示,一切正常,但是一旦我实现了while循环,我看到的是一个黑屏,然后是一个选项,由于它"没有响应"几秒后强制关闭.

TextView.setText(updateWords);
Run Code Online (Sandbox Code Playgroud)

我已将其更改为按钮格式(意味着我必须单击按钮才能自动更新),但我希望它更新自己而不是手动点击它.

我是以错误的方式实现while循环吗?

我也试过在单独的函数中调用while循环,但它仍然给我黑屏的虚无.

我一直在阅读有关Handler服务的内容......它有什么作用?Handler服务能否TextView以更安全或更高效的方式更新我的服务?

非常感谢,如果有人能就我应该做些什么给出一些指示.

implementation android loops while-loop logcat

12
推荐指数
1
解决办法
6万
查看次数

PHP错误:ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有缓冲区要冲洗

有人可以保存这两个文件并运行它们并告诉我为什么我收到错误"ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有缓冲区要刷新".我试着用Google搜索,它说我必须使用ob_start(); 但是当我这样做时,它不会逐行打印,而是在完成后从FOR循环返回整个对象.我是PHP的新手,所以我不知道还有什么地方要看..

test_process.php

// This script will write numbers from 1 to 100 into file
// And sends continuously info to user
$fp = fopen( '/tmp/output.txt', 'w') or die('Failed to open');
set_time_limit( 120);
ignore_user_abort(true);

for( $i = 0; $i < 100; $i++){
    echo "<script type=\"text/javascript\">parent.document.getElementById( 'foo').innerHTML += 'Line $i<br />';</script>";
    echo str_repeat( ' ', 2048);
    flush();
    ob_flush();
    sleep(1);
    fwrite( $fp, "$i\n");
}

fclose( $fp);
Run Code Online (Sandbox Code Playgroud)

main.html中

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" charset="utf-8"></script>

        <style type="text/css" media="screen">
            .msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
            .new{ …
Run Code Online (Sandbox Code Playgroud)

php ajax flush while-loop

12
推荐指数
2
解决办法
2万
查看次数

Wordpress:在WHILE循环中获取current_post索引号

如何index number在WHILE循环中获取当前帖子?

$index_query = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'modified', 'posts_per_page' => '-1', 'order' => 'DESC' ) );
while ( $index_query->have_posts() ) : $index_query->the_post();

    // echo current post index number

endwhile;
Run Code Online (Sandbox Code Playgroud)

尝试了以下,但没有结果.

$index_query->post->current_post;
Run Code Online (Sandbox Code Playgroud)

任何建议非常感谢!

wordpress while-loop

12
推荐指数
2
解决办法
1万
查看次数

如何编写一个简单的Java程序,找到两个数字之间最大的公约数?

这是一个问题:

"编写一个名为gcd的方法,它接受两个整数作为参数,并返回两个数字的最大公约数.两个整数a和b的最大公约数(GCD)是a和b两者的最大整数.任何数字和1的GCD是1,任何数字的GCD和0都是该数字.

计算两个数字的GCD的一种有效方法是使用Euclid算法,该算法表明以下内容:

GCD(A, B) = GCD(B, A % B) 
GCD(A, 0) = Absolute value of A"
Run Code Online (Sandbox Code Playgroud)

我真的很困惑如何解决这个问题.我只想提供一些提示和提示,告诉我到目前为止我在程序中做错了什么.(我必须放入扫描仪,这是我老师的要求.)不要给我一个完整的代码,因为我有点想自己解决这个问题.也许只是给我一个暗示我如何结合你在上面看到的这个公式.(如果你想知道为什么我输入== 0,那是因为我认为如果你有两个数字,比如0和90,他们的GCD会是0吗?)

此外,我的代码必须包括while循环......如果循环我会更喜欢...

提前致谢!:)

我目前的计划:

public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int a = console.nextInt();
        int b = console.nextInt();
        gcd (a, b);
    }

    public static void gcd(int a, int b) {
        System.out.print("Type in two numbers and I will print outs its Greatest Common Divisor: ");
        int gcdNum1 = console.nextInt();
        int gcdNum2 = console.nextInt();
        while (gcdNum1 == 0) { …
Run Code Online (Sandbox Code Playgroud)

java loops while-loop greatest-common-divisor

12
推荐指数
2
解决办法
13万
查看次数

如何使用函数的返回值作为在python中返回元组的条件

我一直在寻找这样的东西,但我找不到它.

一些背景

我使用opencv从视频文件中检索帧.通常人们会在无限循环中这样做:

while (True):
    s, img = cv.read()
Run Code Online (Sandbox Code Playgroud)

要么

for i in xrange(10000): #just a big number
    s, img = cv.read()
Run Code Online (Sandbox Code Playgroud)

现在我想要检索所有帧并在没有更多帧时退出循环.但是我在python中的技能不够强,无法做我想做的事情.

我想知道什么

read函数(或方法,我不知道它们是如何在python中调用的)返回一个元组:第一个表示操作成功,第二个表示返回的帧.我想在元组的第一个元素为false时打破while循环.有C背景,我想也许这会起作用:

while ((success, img = capture.read())[0]):
    #do sth with img
Run Code Online (Sandbox Code Playgroud)

我认为这会在成功失败时打破循环.但事实并非如此.然后我想也许这会奏效:

while ((success, img = capture.read()).success):
    #do sth with img
Run Code Online (Sandbox Code Playgroud)

它也没用.我不想做类似的事情

while(True):
    s, i = capture.read()
    if (s == False):
        break
Run Code Online (Sandbox Code Playgroud)

如何测试条件while,而不是if成功中断?

python conditional opencv while-loop python-2.7

12
推荐指数
3
解决办法
2167
查看次数

如何告诉CasperJS循环浏览一系列页面

我尝试让CasperJS实现以下目标:

  • 浏览按日期顺序命名的一系列页面.
  • 在每个页面上,找到PDF链接.
  • 下载PDF.

我得到了一些工作代码,但我不明白CasperJS是如何经历事件序列的.

例如,在下面的代码示例中,CasperJS尝试处理第2步,并抛出"ReferenceError:找不到变量:formDate",而第1步由于某种原因根本不执行.

我的推理有什么问题?

在我看来,while循环以与casper.then方法不同的速度执行.

casper.start();

casper.thenOpen('http://www.example.com', function() {
    this.echo(this.getTitle());
});

casper.then(function() {

    var start = new Date('2013-01-01T00:00:00');
    var end = new Date('2013-01-31T00:00:00');

    while(start < end) {

          // step 1: define formDate  
          casper.then(function() {
            var formDate = start.getFullYear()+"-"+("0" + (start.getMonth() + 1)).slice(-2) +"-"+("0" + start.getDate()).slice(-2) ;
            casper.echo(formDate);

          });

          // Step 2: open the page and download the file
          casper.thenOpen('http://www.example.com/' + formDate, function() {

                        var url = this.getElementAttribute('div#pdffulllink a.pdf', 'href');
                        this.echo(url);
                        this.download(url, 'Downloaded_' + …
Run Code Online (Sandbox Code Playgroud)

javascript while-loop casperjs

12
推荐指数
1
解决办法
5540
查看次数

虽然每个都被弃用,但Foreach更换需要花费更多时间

使用PHP 7.2,each不推荐使用.文件说:

警告此功能自PHP 7.2.0起已废弃.非常不鼓励依赖此功能.

我正在努力调整电子商务应用程序并将所有while-each循环转换为(假设)等效foreach.

正如您在下面看到的,我已经用等效的替换了所有的reset&while循环foreach.

它工作得很好.但是,我们有一个客户在她的购物车中有很长的商品列表,试图结帐并抱怨她从服务器收到错误502.我试图重现它,发现只有她的购物车失败,加载结帐页面需要2分钟,然后加载502次.然后我开始调试我最近修改过的很多文件,试用和错误,直到我发现问题出在这个特定文件和特定功能上.每当我将第一个foreach循环切换回while循环时,客户可以在不到一秒的时间内加载结帐页面.切换回foreach- 再次需要几分钟,但PHP在结束执行之前超时.

我做的是,输出执行过程中的测试foreachVS的while循环(var_dump $products_id和$this->contents例如),他们似乎都相同.我已经重写了代码以使其顺利运行并保持PHP 7.2兼容,但我仍然无法弄清楚为什么会发生这种情况.

这是完整的功能:

function get_content_type() {
  $this->content_type = false;

  if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {

    // reset($this->contents);
    // while (list($products_id, ) = each($this->contents)) {
    foreach(array_keys($this->contents) as $products_id) {

      if (isset($this->contents[$products_id]['attributes'])) {
        // reset($this->contents[$products_id]['attributes']);
        // while (list(, $value) …
Run Code Online (Sandbox Code Playgroud)

php each performance foreach while-loop

12
推荐指数
1
解决办法
2687
查看次数