while循环重复打印线 - 打破?

时间:2017-06-11 23:41:24

标签: python printing while-loop

如果字符数不是11,如何停止此代码重复打印消息?我希望它重复循环,直到键入正确数量的字符,但它不会这样做。

phone = input(str("enter the phone number"))

while len(phone) !=11:
    if len(phone)>11:
        print("you entered too many numbers")
    elif len(phone)<11:
        print("you entered too few numbers")
else:
    print ("correct phone number length")

2 个答案:

答案 0 :(得分:0)

如果电话号码的长度不是11个号码,您可以尝试再次提示用户输入。

while True:
    phone = input(str("enter the phone number"))
    if len(phone) > 11:
        print("you entered too many numbers")
    elif len(phone) < 11:
        print("you entered too few numbers")
    else:
        break

print ("correct phone number length")

答案 1 :(得分:0)

while True:
    phone = str(input("enter the phone number"))
    if len(phone) == 11:
       break
    if len(phone)>11:
       print("you entered too many numbers")
    elif len(phone)<11:
       print("you entered too few numbers")