For example, % python lab5-bonus.py It is dark. ? lantern You are enlightened. ?
ID: 3623333 • Letter: F
Question
For example,
% python lab5-bonus.py
It is dark.
? lantern
You are enlightened.
? look
This room is decorated in the Early Barnyard style.
? north
This room is unnaturally cold. There are dogs playing poker here.
? north
You can't go any further.
? south
This room is decorated in the Early Barnyard style.
? quit
Code to Modify
dark = True
while True:
if dark == True:
print 'It is dark.'
cmd = raw_input('? ')
if cmd == 'eat':
print 'Nom nom nom.'
elif cmd == 'drink':
print 'Glug glug glug *burp*'
elif cmd == 'look':
if dark == True:
print 'Did I mention that it was dark?'
else:
print 'You are in a maze of twisty little passages, all alike.'
elif cmd == 'lantern':
print 'You are enlightened.'
dark = False
elif cmd == 'quit':
break
else:
print "I don't know that command."
Explanation / Answer
dark = True
x,y = 0,0 # starting coordinates
min_x, max_x = 0,2 # west and east boundaries of the map
min_y, max_y = 0,2 # south and north boundaries of the map
# The effect of each direction
directions = {'north':(0,1),
'south':(0,-1),
'east':(1,0),
'west':(-1,0)}
# Put in your own room descriptions here.
# There's one description for each coordinate on the map.
descriptions = {(0,0) : "You are in the living room.",
(0,1) : "You are in the quirky room.",
(0,2) : "You are in the horrible room.",
(1,0) : "You are in a room with cheese.",
(1,1) : "You are in a life insurance office.",
(1,2) : "You are in a television studio.",
(2,0) : "You are in the jury box in a courtroom.",
(2,1) : "You are at a crime scene.",
(2,2) : "You are in a jail cell."}
while True:
if dark == True:
print "It is dark."
cmd = raw_input('? ')
if cmd == 'eat':
print 'Nom nom nom.'
elif cmd == 'drink':
print 'Glug glug glug *burp*'
elif cmd == 'look':
if dark == True:
print 'Did I mention that it was dark?'
else:
print descriptions[x,y]
elif cmd == 'lantern':
print 'You are enlightened.'
dark = False
elif cmd == 'quit':
break
elif cmd in directions.keys():
# Change the coordinates by the amount specified in the directions dict
x,y = x + directions[cmd][0], y + directions[cmd][1]
if x > max_x or x < min_x or y > max_y or y < min_y:
print "You can't go any further."
x = max(x,min_x)
x = min(x,max_x)
y = max(y,min_y)
y = min(y,max_y)
else:
if dark == False:
print descriptions[x,y]
else:
print "I don't know that command."
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.