-1

I made this list with a for loop that points errors when yoy choose a name. I'd like to know how can I make it so that the last line finishes with '.' and the others finish with ';'.

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

Result when I type a name like '--- ':

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 

I'd like to make it so that 1 and 2 finish with ';' and 3 with '.'. Thanks!

4 Answers4

1
  • All the existing solutions so far seem pretty poor, this is as print is expensive to call.

  • errors_list.index(i) runs in O(n) time making your solution run in O(n^2) time. You can improve this, to O(n) time, by using enumerate.

  • You can also think of what you're doing simply as concatenating values of a list and adding a period.

I would use:

errors = [f'     {i}- {error}' for i, error in enumerate(errors_list, 1)]
print(';\n'.join(errors) + '.')
Peilonrayz
  • 3,129
  • 1
  • 25
  • 37
0

Extending Roman Perekhrest's answer, enumerate has an optional parameter 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 '.'))

additionaly with Python 3.6+ you can use f-strings instead of 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 '.'}")
asikorski
  • 882
  • 6
  • 20
  • Thanks! Learned a lot with both your answers. I just don't get what's the 'start' parameter for. It seems to change nothing –  Jul 19 '19 at 11:44
  • Glad to help! Remember to mark an answer as a solution if you think your problem is solved :) – asikorski Jul 19 '19 at 11:50
  • Also, for future readers: `start=1` changes the starting value that appears at the beginning of each line. Roman's solution starts counting from 0 and performs two redundant `i+1`'s on each iteration. – asikorski Jul 19 '19 at 11:51
-1

Instead of:

for i in errors_list:
    print("     " + str(errors_list.index(i) + 1) + "- " + i + ".")

do

s = len(errors_list)
for e, i in enumerate(errors_list):
    ending = ";" if e + 1 < s else "."
    print("     " + str(errors_list.index(i) + 1) + "- " + i + ending)

EDIT: to those jumping to the gun - OP did write in a title comma, but he used semicolon (;) twice (!) in a question itself.

Radosław Cybulski
  • 2,952
  • 10
  • 21
  • Thanks! I already used one that's simpler though. Yes, I did write comma in the title because I'm not a native english speaker and I didn't remember the name for semicolon. –  Jul 19 '19 at 11:28
-1

Simply with enumerate function:

errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
...
for i, err in enumerate(errors_list):
    print("     {}- {}{}".format(i+1, err, ';' if i+1 != len(errors_list) else '.'))

The crucial loop will output:

    1- It has no letters;
    2- It has symbols;
    3- The last letter is a space.
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105