相关疑难解决方法(0)

在Python中模拟do-while循环?

我需要在Python程序中模拟do-while循环.不幸的是,以下简单的代码不起作用:

list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None

while True:
  if element:
    print element

  try:
    element = iterator.next()
  except StopIteration:
    break

print "done"
Run Code Online (Sandbox Code Playgroud)

而不是"1,2,3,完成",它打印以下输出:

[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', '  File "test_python.py", line 8, in <module>
    s = i.next()
', 'StopIteration
']
Run Code Online (Sandbox Code Playgroud)

我能做些什么来捕获'stop iteration'异常并正确地打破while循环?

以下将伪代码示为可能需要这样的事物的示例.

状态机:

s = ""
while True :
  if state is STATE_CODE :
    if "//" in s :
      tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
      state = …
Run Code Online (Sandbox Code Playgroud)

python while-loop do-while

731
推荐指数
13
解决办法
120万
查看次数

而(1)Vs. for while(True) - 为什么会有区别?

这个关于perl中无限循环的问题很感兴趣:while(1)vs.for(;;)有速度差吗?,我决定在python中运行类似的比较.我期望编译器会为while(True): pass和生成相同的字节代码while(1): pass,但实际上并非python2.7中的情况.

以下脚本:

import dis

def while_one():
    while 1:
        pass

def while_true():
    while True:
        pass

print("while 1")
print("----------------------------")
dis.dis(while_one)

print("while True")
print("----------------------------")
dis.dis(while_true)
Run Code Online (Sandbox Code Playgroud)

产生以下结果:

while 1
----------------------------
  4           0 SETUP_LOOP               3 (to 6)

  5     >>    3 JUMP_ABSOLUTE            3
        >>    6 LOAD_CONST               0 (None)
              9 RETURN_VALUE        
while True
----------------------------
  8           0 SETUP_LOOP              12 (to 15)
        >>    3 LOAD_GLOBAL              0 (True)
              6 JUMP_IF_FALSE            4 (to 13)
              9 POP_TOP             

  9          10 JUMP_ABSOLUTE            3
        >>   13 POP_TOP …
Run Code Online (Sandbox Code Playgroud)

python

110
推荐指数
3
解决办法
7万
查看次数

PYTHON 2.7.3中的布尔运算符

我正在编写一个程序,我要求用户输入.

我希望python检查输入是否为数字(不是单词或puntuation ...),如果它是一个数字,表示我的元组中的对象.如果3个条件中的一个导致False,那么我希望用户为该变量提供另一个值.这是我的代码

colour={'yello':1, 'blue':2, 'red':3, 'black': 4, 'white': 5, 'green': 6}
height_measurements=('centimeter:1', 'inch:2', 'meter:3', 'foot:4')
weight_measurements=('gram:1', 'kilogram:2', 'pounds:3')
print height_measurements
hm_choice = raw_input('choose your height measurement').lower()
while not hm_choice.isdigit() or hm_choice > 0 or hm_choice < len(height_measurements) :
    hm_choice = raw_input('choose your height measurement').lower()        
print weight_measurements
wm_choice = raw_input('choose your weight measurement').lower()
while not wm_choice.isdigit() or wm_choice > 0 or wm_choce < len(weight_measurements) :
    wm_choice = raw_input('choose your weight measurement').lower()
Run Code Online (Sandbox Code Playgroud)

当我把它测试时,无论我放入什么,它都会让我不断地为height_measurement插入输入

请检查我的代码并为我更正.如果你愿意,请为我提供更好的代码.

python boolean-logic python-2.7

0
推荐指数
1
解决办法
848
查看次数

标签 统计

python ×3

boolean-logic ×1

do-while ×1

python-2.7 ×1

while-loop ×1