0

What I am trying to do is make it so the user enters a name, which the program then takes that, adds the word Move to it and then grabs a set named as *name*Move.

Here is my code thus far, with what I want to do within **:

fredMove = set(["move1","move2","move3","move4"])  #different data
joeMove = set(["move","move","move3","move4"])     #for each name
chrisMove = set(["move1","move2","move3","move4"]) #this is just
timMove = set(["move1","move2","move3","move4"])   #easier to write out
#I have many more lists, as well

name = input("What is the name you are looking for? ").lower

def moveChecking(move1,move2,move3,move4,name):
    if (move1 not in *name*Moves):
        print("Move 1 not found!")
    if (move2 not in *name*Moves):
        print("Move 2 not found!")
    if (move3 not in *name*Moves):
        print("Move 3 not found!")
    if (move4 not in *name*Moves):
        print("Move 4 not found!")

move1 = input("Enter move 1 = ")
move2 = input("Enter move 2 = ")
move3 = input("Enter move 3 = ")
move4 = input("Enter move 4 = ")

moveChecking(move1,move2,move3,move4,name)

Any way to do what I want in a way that lets me avoid creating moveChecking() for every person's name?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
rainydevbs
  • 152
  • 10

3 Answers3

2

Use a dictionary.

moves = {
    "fred":  set(["move1","move2","move3","move4"])
    "joe":   set(["move1","move2","move3","move4"])
    "chris": set(["move1","move2","move3","move4"])
    "tim":   set(["move1","move2","move3","move4"])
}

name = input("What is the name you are looking for?")
for i in range(1, 5):
    move = input("Enter move {} = ".format(i))
    if move not in moves[name]:
        print("Move {} not found!".format(i))
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

Create a dictionary of the move sets then access them by name

moveDict = {'fred': fredMove, 'joe':joeMove } #etc

And then in moveChecking use

if move1 not in moveDict[name]:
    #...
Holloway
  • 6,412
  • 1
  • 26
  • 33
1

They're are many, many ugly ways to do what you want as you've discribed it (essentially doing getattr at the module level), however this seems more like a case of bad design. Instead of having a bunch of sets assigned directly to named variables, why not use a dictionary instead, with they key as the name and the value as the set? EX:

moves = {'timMove':set(["move1","move2","move3","move4"]),
 ....}

And then you can simply get the move set like so:

try:
    move_set = moves[name]
except KeyError:
     # handel a bad name here
aruisdante
  • 8,875
  • 2
  • 30
  • 37