该文件必须包含3到10行文本。也就是说,3是可接受的最小行数,10是最大行数。
所有行必须包含完全相同的字符数。
每行必须包含3到10个字符。也就是说,3是可接受的最小字符数,10是最大值。每行的字符数不必等于文件中的行数。
唯一可接受的字符是'x','X','y','Y'和'_'。
correct_string = False
while correct_string is False:
string = input("Enter a string? ")
if len(string) != len(string):
print("Error: string must have the same number of characters.")
else:
incorrect_char = False
for i in string:
if i != "X" and i != "x" and i != 'Y' and i != 'y' and i != "_":
incorrect_char = True
if incorrect_char is False:
correct_string = True
else:
print("Invalid Character. Contains characters other than 'X', 'x', 'Y' 'y',and '_'")
答案 0 :(得分:0)
with open("my_file.txt") as f:
lines = f.readlines()
assert all(len(line) == len(lines[0]) for line in lines[1:])
assert len(lines) == len(lines[0])
也许......不确定你的要求
答案 1 :(得分:0)
def check_file(f):
for i, line in enumerate(f):
if i > 10: return False
for j, c in enumerate(line.rstrip('\n')):
if j > 10: return False
if c not in 'xXyY_':
return False
if j < 3: return False
if i < 3: return False
return True