gpt4 book ai didi

python - 如何自动创建变量?

转载 作者:行者123 更新时间:2023-11-28 21:21:25 25 4
gpt4 key购买 nike

<分区>

我刚开始使用 Python,目前正在制作蛇梯游戏。这是我的代码:

    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")

这是我得到的:

    >>> 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
>>>

我正在尝试弄清楚为什么它没有将“Alvin”声明为变量或者为什么我不能将其打印出来。我不确定我是否犯了一个愚蠢的错误。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com