Python 3.4 while循环增量

时间:2015-07-21 04:42:50

标签: python while-loop increment

  x = Flase
  while !x :
     a = 0
     print(a)
     a++
     pass
     if a == 10:
       x = True
    else:
       continue  

我在" a ++"时收到错误。我使用的是visual studio 2013社区,它给了我一个红色下划线" a ++"就在"通过"

2 个答案:

答案 0 :(得分:2)

Python不支持++。你可以这样做

........     
array(
            'name'=>'result_severnity',
            'type' => 'raw',
            'value'=> '$data->getResultSevernity()',
            'filter'=>$resultSevernityList,
            'headerHtmlOptions'=>array('style'=>'width:300px;text-align:center;'),
....

model:
    public function getResultSevernity($type="")
        {
            $resultSevernity = $this->result_severnity;
            switch($resultSevernity)
            {
                case '0':
                    $returnText = t("common","severnity_none");
                    break;
                case '10':
                    $returnText = t("common","severnity_minor");
                    break;
                    ......
                }
            if($type=="HTML") {
                $class = "severnity_".$resultSevernity;
                $returnText = cHtmlTag($returnText, 'span', array('class'=>$class));
            }
            return $returnText;
        }

答案 1 :(得分:0)

Python中没有++--等语法,但您可以执行+= 1-= 1

你的程序中存在一些逻辑错误

x = False
a = 0  # a should be initialized here. If it is initialized in loop it will be a never-ending loop
while not x :

   print(a)
   a+=1
   if a == 10:
     x = True
   else:
     continue

备注:

  1. 检查您写错了False上的拼写

  2. 不需要pass

  3. 而不是使用!,您应该在Python中使用not

  4. 更简单,更pythonic的方式是使用forrange

    for a in range(10):
        print(a)