I just started with python and I am currently making a snakes and ladders game. Here is my code:
nameList = []
def players_name():
x = 0
input ("""-Press enter to start inputting players' name.
-Press enter after each name inputed to confirm the name to players list.
-After last name inputed, press enter again without any input to finish.""")
name = input ("Input players' name:")
while name != "" or len (nameList) <= 1:
x = x + 1
if name in nameList:
print (name,"is already in list, please input a different name.")
name = input ("")
elif name =="" and len (nameList) <= 1:
print ("A minimum of 2 players are require to start the game.")
name = input ("")
else:
nameList.append(name)
exec("{} = {}".format(name, "1"))
numList.append("{0}".format (x))
name = input ("")
nameList.sort()
numList.sort()
print ("There are",len (nameList),"players inputed in your players list!")
print ("Your players list:",nameList,"\n")
This is what I got:
>>> players_name()
-Press enter to start inputting players' name.
-Press enter after each name inputed to confirm the name to players list.
-After last name inputed, press enter again without any input to finish.
Input players' name:Alvin
James
George
There are 3 players inputed in your players list!
Your players list: ['Alvin', 'George', 'James']
>>> print(Alvin)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print(Alvin)
NameError: name 'Alvin' is not defined
>>>
I am trying to work out why didn't it declare "Alvin" as a variable or why can't i print it out. I am not sure if I am making a silly mistake.