Current assignment is building a basic text adventure. I'm having trouble with the following code. The current assignment uses only functions, and that is the way the rules of the assignment state it must be done.
def make_selections(response):
repeat = True
while repeat == True:
selection = raw_input('-> ')
for i, v in enumerate(response):
i +=1 # adds 1 to the index to make list indices correlate to a regular 1,2,3 style list
if selection == i:
print v[1]
else:
print "There's an error man, what are you doing?!?!?"
firstResponse = 'You chose option one.'
secondResponse = 'You chose option two.'
thirdResponse = 'You chose option three.'
responses = [(0, firstResponse), (1, secondResponse),( 0, thirdResponse)]
make_selections(responses)
My intention in that code is to make it so if the user selects a 1, it will return firstResponse, if the user selects 2 it will return secondResponse, etc.
I am basically just bug testing the code to make sure it produces the appropriate response, hence the "Error man..." string, but for some reason it just loops through the error message without printing the appropriate response string. Why is this?
I know that this code is enumerating the list of tuples and I can call them properly, as I can change the code to the following and get the expected output:
for i, v in enumerate(response):
i += 1 # adds 1 to the index to make list indices correlate to a regular 1,2,3 style list
print i, v
Also, two quick asides before anyone asks:
- I know there is currently no way to get out of this
whileloop. I'm just making sure each part of my code works before I move on to the next part. Which brings me to the point of the tuples. - When I get the code working, a 0 will produce the response message and loop again, asking the user to make a different selection, whereas a 1 will produce the appropriate response, break out of the loop, and move on to the next 'room' in the story... this way I can have as many 'rooms' for as long of a story as I want, the player does not have to 'die' each time they make an incorrect selection, and each 'room' can have any arbitrary amount of options and possible responses to choose from and I don't need to keep writing separate loops for each room.
enumerate(response, 1)will give you the 1-based indices without needing thati += 1. Also, you don't needwhile repeat == True:, justwhile repeat:.type(selection)compared totype(i)? (2)responsescontains tuples, and you're never using the first index of those tuples (i.e., you're not using the0of(0, firstResponse)). (3) If you're working with numbered options, do you even need that first index? surely there's an easier way to access the correct item on theresponselist, using the raw_input, without using a loop...iandselectionwhere different types and fixed it by changing the relevant line toselection = int(raw_input('-> ')). I am not using the tuples currently because I haven't written that part of the code yet. I was planning on using them as a flag to to an if statement to break out of the loop and allow the code to continue in order for the player to progress into the next room.