继续使用的正确方法是什么?

What is the proper way to use continue?

我 运行 在使用 continue 后完成另一次迭代时遇到问题。目标是获得大于或等于 3 的整数输入。我不希望脚本对用户出错,相反我想要求另一个输入。

while True:
    try:
        sides = int(raw_input("Marty wants to draw you a shape. How many sides will the shape have?"))
    except ValueError:
        print "Marty needs an integer, try again."
        continue
    if sides < 2:
        print "Marty needs a number greater than 2, try again."
        continue
    else:
        break

使用continue 两次时是否出现问题?任何有关正确使用 continue 的建议都会很棒。就目前而言,它要求用户输入。如果给出的不是整数,它会要求另一个输入。如果给定 2 它什么都不做,甚至不打印,更不用说再试一次输入了。

问题不在于您对继续的使用,而在于您对输入的评估。而不是你拥有的,试试:

if sides <= 2:
    print 'Marty needs a number greater than 2, try again.'
    continue

或:

if sides < 3: