【问题标题】:How do I get an input to equal a certain amount of characters?如何让输入等于一定数量的字符?
【发布时间】: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")

【问题讨论】:

标签: python validation string-length


【解决方案1】:

要获取字符串中的字符数,可以使用len(postcode)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
相关资源
最近更新 更多