【发布时间】:2017-02-26 16:46:46
【问题描述】:
我们的老师告诉我们编写一个程序来验证邮政编码是否为北爱尔兰邮政编码。这意味着它必须包含字母“BT”并且必须等于 8 个字符。在下面的代码中,我设法让字母部分正常工作。但是,他没有详细说明如何使输入等于 8 个字符。他提到使用 .length() 和验证(尝试和除外),但我不确定如何使用 .length() 来获取 8 个字符。这是我的代码:
postcode = input("Please enter an Northern Ireland postcode:")
BT = "BT"
while BT not in postcode:
postcode = input("That isn't a Northern Ireland postcode. Try again:")
print("This is a Northern Ireland postcode")
【问题讨论】:
-
if len(postcode) != 8? -
试过了,效果很好,谢谢。
标签: python validation string-length