我用 for 循环制作了这个列表,当你选择一个名字时会指出错误。我想知道如何才能使最后一行以 '.' 结束。其他人以';'结束。
while True:
if len(errors_list) != 0:
print("Your name has thesse errors::")
for i in errors_list:
print(" " + str(errors_list.index(i) + 1) + "- " + i + ".")
print("Try again.")
errors_list.clear()
name = input("My name is ").title()
choose_name(name)
else:
print("Nice to meet you, " + fname + " " + sname + ".")
break
当我输入一个像“---”这样的名字时的结果:
Your name has these errors:
1- It has no letters.
2- It has symbols.
3- The last letter is a space.
Try again.
My name is
我想让 1 和 2 以 ';' 结束 和 3 与 '.'。谢谢!
扩展 Roman Perekhrest 的答案, enumerate 有一个可选参数start
:
errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
for i, err in enumerate(errors_list, start=1):
print("\t{}- {}{}".format(i, err, ';' if i < len(errors_list) else '.'))
另外在 Python 3.6+ 中,您可以使用f-strings而不是format
:
errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
for i, err in enumerate(errors_list, start=1):
print(f"\t{i}- {err}{';' if i < len(errors_list) else '.'}")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句