Typically, one does not optimize a program (make it run faster or with less use of memory) until after it is running, well-debugged, and well-tested. (Of course, you still have to test again after each optimizing modification.) Here is an optimization that we could make to the adventure game. Currently, showRoom compares the room variable to each possible room—even if it matched earlier. Python gives us a way of only testing once, by using an elif instead of later if statements. The statement elif means “else if.” You only test the elif statement if the earlier if was false. You may have as many elif statements as you like after an if. You might use it like this:

```
if (room == ‘‘Porch’’):
showPorch()
elif (room == ‘‘Kitchen’’):
showKitchen()
```

Rewrite the showRoom method more optimally by using elif.

```
def showRoom(room):
printNow("===========")
if room == "Porch":
showPorch()
elif room == "Entryway":
showEntryway()
elif room == "Kitchen":
showKitchen()
elif room == "LivingRoom":
showLR ()
elif room == "DiningRoom":
showDR ()
```

Computer Science & Information Technology

You might also like to view...

Virtual machine ________ are used to capture the state, data, and configuration of a VM

Fill in the blank(s) with correct word

Computer Science & Information Technology

Give the following class template, what changes need to be made to the default constructor definition?

template class containerClass { public: containerClass(); containerClass(int newMaxSize); containerClass(const containerClass& source); ~containerClass(); T getItem(); int getCount(); int getSize(); void addItem(T item); private: T *bag; int maxSize, count; }; containerClass::containerClass() { maxSize = 10; bag = new int[maxSize]; count=0; } a. add the template prefix b. change the type of the dynamic array allocation to T c. add the following the class name before the scope resolution operator d. all of the above e. none of the above

Computer Science & Information Technology