i is reassigned at each iteration of the for loop, which overwrites all previous assignments, including those made in the body like your assignment i -= 1. So use a while loop instead.
Also a few suggestions:
- You should validate all your inputs, including the number of entries to the phone book.
- You should only catch the most specific exceptions (
ValueError here), so you rarely need to catch all exceptions (BaseException)—when you do not know specifically which exceptions to catch, catch Exception, not BaseException. Your expression-less except clause except: catches all exceptions, so it is equivalent to the except clause except BaseException:. BaseException catches (besides Exception) SystemExit, KeyboardInterrupt, and GeneratorExit, which you usually do not want to catch (cf. the Python exception hierarchy).
- You should follow the Python naming convention
lower_case_with_underscores for function and variable names, not mixedCase (cf. PEP 8).
The new program:
while True:
try:
user_input = input("Enter number of entries to phone book: ")
i = int(user_input)
except ValueError:
print("Invalid input, enter a number.")
else:
break
phone_book = {}
while i:
try:
user_input = input("Enter contact (name, 2 spaces, phone number): ")
name, phone_number = user_input.split(" ")
except ValueError:
print("Invalid input, enter 2 spaces between name and phone number.")
else:
phone_book[name] = phone_number
i -= 1
print(phone_book)
An algorithmic pattern appears therefore you can create a function that encapsulates it so that you do not repeat yourself (cf. the DRY principle):
def prompt(condition, message, warning, validate, process, new):
output = None
while condition:
try:
user_input = input(message)
valid_input = validate(user_input)
except ValueError:
print(warning)
else:
output = process(valid_input)
condition = new(condition)
return output
condition = True
message = "Enter number of entries to phone book: "
warning = "Invalid input, enter a number."
validate = lambda x: int(x)
process = lambda x: x
new = lambda x: False
entry_count = prompt(condition, message, warning, validate, process, new)
condition = entry_count
message = "Enter contact (name, 2 spaces, phone number): "
warning = "Invalid input, enter 2 spaces between name and phone number."
validate = lambda x: x.split(" ") if x.index(" ") else None
phone_book = {}
process = lambda x: phone_book.update({x[0]: x[1]})
new = lambda x: x - 1
prompt(condition, message, warning, validate, process, new)
print(phone_book)