[Tutor] Unknown Cause of Error
Steven D'Aprano
steve at pearwood.info
Sat May 11 08:12:56 CEST 2013
On 11/05/13 12:13, Jack Little wrote:
> I have a slight problem. My program will not open.
What do you mean "open"? How are you trying to open it? What editor are you opening it in?
Or do you mean your program will not *run*? How are you trying to run it?
>On top of that, I have written similar programs all to no avail. I am creating a text adventure and want there to be different rooms. Here is my code:
>
>
>
> 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()
I can see two obvious problems here. First, you define a function, "menu", but do not call it when the program runs. You must call the function, or it will not do anything.
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.
Fortunately, you can fix both problems with one edit: *indented* code is part of the function, code that is not indented is not. So:
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()
--
Steven
More information about the Tutor
mailing list