0

In my python game I'm playing around with I have a menu where you can select to either play the game, load a saved game, save a game, and quit. The input of numbers works fine but when you accidentally or purposefully enter a letter or symbol it returns the error:

Choice = eval(input())
File "string", line 1, in module
NameError: name 'y' is not defined"

How would I go about fixing this issue?

Function where the error occurs:

def DisplayMenu():
    print('1.  Start new game')
    print('2.  Load game')
    print('3.  Save game')
    print('9.  Quit')
    print('Please enter your choice: ')

def GetMainMenuChoice():
    Choice = eval(input())
    return Choice

if not (Choice in ['1','2','3','4','9']):
    print("\n Invalid choice. Enter your option again. \n")
    Choice = False
else:
    Choice = True 
ismail
  • 46,010
  • 9
  • 86
  • 95
Hoxephos
  • 71
  • 1
  • 1
  • 6
  • Please fix your formatting. Indent each line with at least 4 spaces for it to appear as code. And please remove the preceding `>` as that acts as a quotation. Otherwise we cannot accurately read and help you with your code. – Cory Kramer Mar 01 '15 at 17:02
  • don't use eval and worse again it will actually make your code logic incorrect, int can never be equal to a str – Padraic Cunningham Mar 01 '15 at 17:04

1 Answers1

0

You need raw_input if you are using python2, and don't use eval, just cast to int if you need an int etc... Unless you have a variable y defined somewhere when you try to eval the string y you will get the error you see, either way eval should rarely if ever be used. The fact your list is full of strings makes trying to make Choice and int illogical.

choice = raw_input() #  input python3

if choice not in {'1','2','3','4','9'}
   ....

Use lowercase for variable/function names.

You may also find a while loop to be a nicer approach:

def get_main_menu_choice():
    while True:
        choice = raw_input("Please choose an option")
        if choice not in {'1','2','3','4','9'}:
            print("Invalid choice. Enter your option again.")
        else:
            return choice

what-does-pythons-eval-do

Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Sorry about using eval, I've barely gotten into Python. Your solutions works very well; however when I enter any of the valid inputs it just reprints the menu and doesn't execute what it is meant to. Any ideas? – Hoxephos Mar 02 '15 at 17:56
  • Before your amazing update to the menu choice (Thanks again) when you entered the menu choice it would run the relevant functions and make it work as intended; however with your amendments it now doesn't run the functions after entering your choice. What I want it to do is to run the functions when the relevant option is selected with your new fix to the menu. – Hoxephos Mar 02 '15 at 19:47
  • Which functions do you want to run? – Padraic Cunningham Mar 02 '15 at 19:48
  • The functions are already implemented into the code. For example Option One will run "Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake = SetUpGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake, True) PlayGame(Cavern, TrapPositions, MonsterPosition, PlayerPosition, FlaskPosition, MonsterAwake)" which allows the actual game to be played. – Hoxephos Mar 02 '15 at 19:52
  • so 1 ,2 ... all correspond to running a certain function? – Padraic Cunningham Mar 02 '15 at 19:54
  • Yes and they have all be assigned and work. I just needed to run the menu choice without an input of letters breaking it and causing an error. – Hoxephos Mar 02 '15 at 19:57
  • Can you add the code too pastebin, I am pretty sure a dict will be a good solution but seeing the functions and how they are called will confirm – Padraic Cunningham Mar 02 '15 at 19:58
  • what do you want to do with choice after the function ends? – Padraic Cunningham Mar 02 '15 at 20:05
  • After inputting the choice it should automatically load the functions. The choice returns to 0 after so you can reuse the menu. – Hoxephos Mar 02 '15 at 20:09
  • Really you should have all your actual game code in a while loop, calling the appropriate functions along the way so for instance the player dies you can load the menu and ask if they want to play again etc.. – Padraic Cunningham Mar 02 '15 at 20:12
  • Yeah... I'm not a very confident coder. This is just a sample code I was given to try and learn more from the deliberate mistakes they put in. – Hoxephos Mar 02 '15 at 20:15
  • Ah ok, well if you want to return a function based on what number the user enters you can do something like this http://pastebin.com/ZnmwK1H1 if there are cases where not all entrie relate to calling a function you would just check that first and do the necessary – Padraic Cunningham Mar 02 '15 at 20:23