[英]How do I get an input to equal a certain amount of characters?
Our teacher has told us to make a program that validates a postcode as a Northern Ireland postcode. 我们的老师告诉我们制作一个程序,以将邮政编码验证为北爱尔兰的邮政编码。 That means it must have the letters "BT" in it and must equal 8 characters. 这意味着它必须包含字母“ BT”,并且必须等于8个字符。 In the code below I managed to get the letters part working. 在下面的代码中,我设法使字母部分起作用。 However, he did not go into detail on how to make the input equal 8 characters. 但是,他没有详细说明如何使输入等于8个字符。 He mentioned using .length() and validation(try and except), but I'm unsure how to use .length() to get 8 characters. 他提到使用.length()和validation(尝试和除外),但是我不确定如何使用.length()获得8个字符。 Here's my code: 这是我的代码:
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")
要获取字符串中的字符数,可以使用len(postcode)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.