Game Development Reference
In-Depth Information
that her cat is fuzzy. If catName is anything else, then we tell the user her cat is not
fuzzy.
But what if we wanted something else besides "fuzzy" and "not fuzzy"? We could put
another if and else statement inside the first else block like this:
if catName == 'Fuzzball':
print('Your cat is fuzzy.')
else:
if catName == 'Spots'
print('Your cat is spotted.')
else:
print('Your cat is neither fuzzy nor
spotted.')
But if we wanted more things, then the code starts to have a lot of indentation:
if catName == 'Fuzzball':
print('Your cat is fuzzy.')
else:
if catName == 'Spots'
print('Your cat is spotted.')
else:
if catName == 'FattyKitty'
print('Your cat is fat.')
else:
if catName == 'Puff'
print('Your cat is puffy.')
else:
print('Your cat is neither fuzzy
nor spotted nor fat nor puffy.')
Typing all those spaces means you have more chances of making a mistake with the
indentation. So Python has the elif keyword. Using elif , the above code looks like this:
if catName == 'Fuzzball':
print('Your cat is fuzzy.')
elif catName == 'Spots'
print('Your cat is spotted.')
elif catName == 'FattyKitty'
print('Your cat is fat.')
elif catName == 'Puff'
print('Your cat is puffy.')
else:
Search WWH ::




Custom Search