Nate Sturtz
Nate Sturtz

Reputation: 41

How do I check if a name is among a list of names in python?

I am writing Oregon trail game and this is the code I have that is causing issues, I don't know why it is having issues. What I want to do is if they enter a name that contains a word that is in a list it will set the variable easter_mode to 1 if they don't then it will set easter_mode to 0. The words that need to be in the list are: (Sturtz, sturtz, Nate, nate) Thank you

#asking name
player_name = input('What is your name:')
while len(player_name) >= 0:
  if len(player_name) > 1:
    print("Weclome" + str(player_name))
    print('Which mode do you want to play?')
    mode_choice = input('(easy) More modes comming soon:')
    break
  if len(player_name) == 1:
    player_name_choice = input(str(player_name)+"? Are you kidding me? Only one letter? You might regreat it (Y/N):")
    if player_name_choice == "y" or player_name_choice == "Y":
      print("Ok Your Choice!!...")
      mode_choice = 'easter'
      break
    if player_name_choice == "n" or player_name_choice == "N":
      player_name = input('What is your name:')
  else:
    print("You do not type anything, try again")
    player_name = input('What is your name:')

#Check Easter Egg Names
easter_names = ["nate sturtz", "Nate Sturtz", "Nate", "nate", "Sturtz", "sturtz"]
if player_name in easter_names:
    easter_mode = 1
else:
    easter_mode = 0
#easter eggs for name

if easter_mode == 1:
  year_set = 2005
  mode_choice = 'easter'
else:
  year_set = input('Enter a year whatever you like:')
  if year_set.isdigit():
    return_num = 0
  else:
    return_num = 1
  while return_num == 1:
    print('Error,please try again!')
    year_set = input('Enter a year whatever you like:')
    if year_set.isdigit():
      return_num = 0
    else:
      return_num = 1
  year_set = int(year_set)

When I run the full file I get

Traceback (most recent call last):
  File "Oregon.py", line 64, in <module>
    player_name = input('What is your name:')
  File "<string>", line 1, in <module>
NameError: name 'nate' is not defined

You can view the full code on Github
https://raw.githubusercontent.com/nsturtz/Oregon-Trail/master/Oregon.py

Upvotes: 1

Views: 457

Answers (2)

Benjamin
Benjamin

Reputation: 546

You'll get this error in Python 2. In Python 2, input() uses the exact value as your enter it.

In your example, you're typing nate and not 'nate'. The former value is a variable name (which is undefined in your code, hence the NameError), whereas the latter is a string.

In Python 3, input() behaves as you assume, and passes a string to your code.

If you are sure that you want to use Python 2, you can replace input() with raw_input() and it will interpret your input as a string rather than a variable name.

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 545963

Under Python 2, you can use raw_input instead of input to prevent Python from interpreting the user input as Python code.

However, since Python 2 is deprecated, I strongly recommend against using it1. Use Python 3 instead, where input works as expected.


1 Except of course to maintain legacy products. But that doesn’t seem relevant here.

Upvotes: 2

Related Questions