[Tutor] Unknown Cause of Error
Alan Gauld
alan.gauld at btinternet.com
Sat May 11 08:56:43 CEST 2013
On 11/05/13 07:12, Steven D'Aprano wrote:
>> def menu():
>> print "Welcome to Tomb Explorer!"
>> print "A game of Exploration from Bulldog Development"
>> print "Press [1] to Play or [2] to Exit"
>> menu1=raw_input(" >> ")
>> if menu1== "2":
>> quit()
>> if menu1== "1":
>> room1()
>> menu()
>
> The second problem is that *inside* the "menu" function you make a
> recursive call to itself. While that is sometimes a legitimate
> technique, this time it will lead to problems as the menu function gets
> called again and again and again and again in an (almost) infinite loop.
To the OP, not Steven...
Beginners often use this technique to deliberately create a loop within
their functions. If that is what you wanted then it's better to use one
of Pythons native loop structures, 'while' or 'for'.
'for' is generally used when the number of iterations is known or can be
calculated. So in your case a 'while' loop is more appropriate.
Typically your code should look like:
while True: # loop 'forever'
# ...display menu
if menu1== "2":
break # exit the while loop
elif menu1== "1":
room1()
elif... # other menu choices.
else:
# display invalid choice error
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list