我有个问题。即使在不同的def函数中,如何使变量保持定义状态?我有一个函数,用于查看有多少个AIs a user wants
,例如,如果他们选择“一”作为他们想要的AIs数量AI1 would be equal 1
。这里定义了AI1:
def AISection():#Same as line 1
global AI1, AI2, AI3
print("How many AIs do you want to play with?\nChoices:\n1. One\n2. Two\n3. Three")
#Asks user next question.
while(True):
#Same as line 4.
UserInput = input("Answer goes here:")
#same as line 6
if(UserInput in ['ONE', 'One', 'one']):
#Same as line 8
AI1 = 1
#Only AI1 will be activated.
break
#Same as line
所以如果他们只choose 'One'
的话。一旦发生这种情况,用户将能够选择多少卡,他们想要的。这是基于他们希望激活多少个AIs,因此在上一段中One
将被选中,因此他们可以从下面代码中的3个不同卡金额中进行选择:
def CardsSection():#Same as line 1
global AI1, AI2, AI3#Makes sure the AIs are equal to something
print("How many cards do you want in your hand?")
#Asks the user for an amount of cards the users wants.
if(AI1 == 1) and (AI2 == 2):
#If they choose 2 AIs.
print("Choices:\n1. Four\n2. Six\n3. Eight")
#This is so an even amount of cards will be distributed from the deck.
elif(AI1 == 1) or (AI1 == 1) and (AI3 == 3):
#If they choose 1 AI or 3 AIs.
print("Choices:\n1. Five\n2. Seven\n3. Nine")
#Same is line 68.
但是当我运行代码时,它到达了def CardsSection
(AI1定义在AI1节之后的那一节)。然后给了我一个错误:
Traceback (most recent call last):
File "python", line 81, in <module>
File "python", line 68, in CardsSection
NameError: name 'AI1' is not defined
问题是我定义了AI1=1,但是我的代码无法识别它。我认为这是因为它的定义不同。我想如果我使用全局“Module”,不管我把AI1,AI2和AI3设置为相等,不管我使用的是什么def函数,都会是这样。我怎样才能使AI1、AI2和AI3等于(在本例中是AISection),而不管我在哪里,它们都是这样?以下是我目前掌握的完整代码:
def StartSection():#The starting section.
print("Do you want to play?\nChoices:\n1. Yes\n2. No")
#Asks the user a question.
while(True):
#Loops if user doesn't answer properly.
UserInput = input("Answer goes here:")
#Prompts the users.
if(UserInput in ['YES', 'Yes', 'yes']):
#If user says yes.
print("Great! Now choose your 'AI Settings'.")
#Praises the user & tells user about next prompt.
break
#Stops the loop. Continues to next prompt.
elif(UserInput in ['NO', 'No', 'no']):
#Else, if User says no.
print("Bye bye!")
#Farewells to the user.
quit()
#Ends Code.
else:
#Else user types neither 'Yes' or 'No'.
print("That is not a choice! Please try again.")
#Tells user to choose again.
print("Here are the choices:\n1. Yes\n2. No")
#Tells user their choices again; goes back to start.
StartSection()#Ends the section of code.
def AISection():#Same as line 1
global AI1, AI2, AI3
print("How many AIs do you want to play with?\nChoices:\n1. One\n2. Two\n3. Three")
#Asks user next question.
while(True):
#Same as line 4.
UserInput = input("Answer goes here:")
#same as line 6
if(UserInput in ['ONE', 'One', 'one']):
#Same as line 8
AI1 = 1
#Only AI1 will be activated.
break
#Same as line 12
elif(UserInput in ['TWO', 'Two', 'two']):
#Same as line 14
AI1 = 1
AI2 = 2
#AI1 and AI2 are activated. AI3 is not activated.
break
#Same as line 12
elif(UserInput in ['THREE', 'Three', 'three']):
#Same as line 14
AI1 = 1
AI2 = 2
AI3 = 3
#All 3 AIs are activated.
break
#Same as line 12
else:
print("That is not a choice! Pleasse try again.")
#Same as line 22
print("Here are your choices:\n1. One\n2. Two\n3. Three")
#Same as line 24
print("You selested %s AIs" % (UserInput[0].upper()+UserInput[1::1].lower()))
#Tells the user that what they select while keeping it in a upper-lower case fashion.
AISection()#Same as line 26
def CardsSection():#Same as line 1
global AI1, AI2, AI3#Makes sure the AIs are equal to something
print("How many cards do you want in your hand?")
#Asks the user for an amount of cards the users wants.
if(AI1 == 1) and (AI2 == 2):
#If they choose 2 AIs.
print("Choices:\n1. Four\n2. Six\n3. Eight")
#This is so an even amount of cards will be distributed from the deck.
elif(AI1 == 1) or (AI1 == 1) and (AI3 == 3):
#If they choose 1 AI or 3 AIs.
print("Choices:\n1. Five\n2. Seven\n3. Nine")
#Same is line 68.
else:
print("Something didn't go right!")
#If they happened to choose neither 1, 2, or 3 AIs.
return StartSection()
#Returns them basck to start.
CardsSection()
我希望这有帮助:)。你知道吗
问题是您没有在AISection()中定义变量AI1、AI2或AI3。您需要在那里将它们设置为0,否则它们就没有定义。你知道吗
我相信您正面临一个scope问题,尽管您将它定义为全局的,但您还没有在函数之外定义它。如果在任何函数之外定义了三个变量,则程序似乎正常工作:
另外请注意,虽然这不是代码检查,但我强烈建议使用用户输入.下()==“一”代替你的列表检查,它将更有效,并接受更多的答案(“一”例如,如果用户曾经想输入)。你知道吗
相关问题 更多 >
编程相关推荐