Python 如何实现当循环(When)和直到循环(Until)

目录

  • 当循环(When)
  • 直到循环(Until)

当循环(When)

直接使用 while 就可以了

print "Please input a number"
a = int(raw_input())

# when
while a != 10:

# do
    print "%s is wrong. Try it again." % (a)
    a = int(raw_input())

print "You got the right number."

直到循环(Until)

python需要再用无限循环(死循环)加break实现直到循环,其中的 while True 相当于 do,if…break 相当于 until

# do
while True:
    print "Please input a number"
    a = int(raw_input())

# until
    if a == 10:
        break

print "You got the right number."

你可能感兴趣的:(Python 如何实现当循环(When)和直到循环(Until))