Checkpoint 7.12.1.
11-9-1: What kind of error will come from trying to access a nonexistent element or one that is out of range?
while
loop in the chapter on iteration:while True:
line = input('> ')
if line[0] == '#':
continue
if line == 'done':
break
print(line)
print('Done!')
> hello there
hello there
> # don't print this
> print this!
print this!
>
Traceback (most recent call last):
File "copytildone.py", line 3, in <module>
if line[0] == '#':
IndexError: string index out of range
startswith
method, which returns False
if the string is empty.if line.startswith('#'):
if
statement using the guardian pattern and make sure the second logical expression is evaluated only where there is at least one character in the string:if len(line) > 0 and line[0] == '#':