I have a question. How to make variables remain defined even if you use different def functions? I have a function that serves to see how many AIs a user wantsand, for example, if they selected "One" for the number of AIs that they wanted AI1 would be equal 1. AI1 is defined here:
def AISection():
global AI1, AI2, AI3
print("How many AIs do you want to play with?\nChoices:\n1. One\n2. Two\n3. Three")
while(True):
UserInput = input("Answer goes here:")
if(UserInput in ['ONE', 'One', 'one']):
AI1 = 1
break
So, if they choose 'One'only AI1 will be equal to 1. Then, as soon as this happens, the user will be able to choose how many cards they need. This is based on how many AIs they wanted to activate. Thus, in the previous paragraph it Onewill be selected, and therefore they will have options for choosing from 3 different card amounts in the code below:
def CardsSection():
global AI1, AI2, AI3
print("How many cards do you want in your hand?")
if(AI1 == 1) and (AI2 == 2):
print("Choices:\n1. Four\n2. Six\n3. Eight")
elif(AI1 == 1) or (AI1 == 1) and (AI3 == 3):
print("Choices:\n1. Five\n2. Seven\n3. Nine")
, def CardsSection ( AISection, 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, . , , def. , "", , AI1, AI2 AI3 , , , def . , , AI1, AI2, AI3 ( AISection), , , ? , :
def StartSection():
print("Do you want to play?\nChoices:\n1. Yes\n2. No")
while(True):
UserInput = input("Answer goes here:")
if(UserInput in ['YES', 'Yes', 'yes']):
print("Great! Now choose your 'AI Settings'.")
break
elif(UserInput in ['NO', 'No', 'no']):
print("Bye bye!")
quit()
else:
print("That is not a choice! Please try again.")
print("Here are the choices:\n1. Yes\n2. No")
StartSection()
def AISection():
global AI1, AI2, AI3
print("How many AIs do you want to play with?\nChoices:\n1. One\n2. Two\n3. Three")
while(True):
UserInput = input("Answer goes here:")
if(UserInput in ['ONE', 'One', 'one']):
AI1 = 1
break
elif(UserInput in ['TWO', 'Two', 'two']):
AI1 = 1
AI2 = 2
break
elif(UserInput in ['THREE', 'Three', 'three']):
AI1 = 1
AI2 = 2
AI3 = 3
break
else:
print("That is not a choice! Pleasse try again.")
print("Here are your choices:\n1. One\n2. Two\n3. Three")
print("You selested %s AIs" % (UserInput[0].upper()+UserInput[1::1].lower()))
AISection()
def CardsSection():
global AI1, AI2, AI3
print("How many cards do you want in your hand?")
if(AI1 == 1) and (AI2 == 2):
print("Choices:\n1. Four\n2. Six\n3. Eight")
elif(AI1 == 1) or (AI1 == 1) and (AI3 == 3):
print("Choices:\n1. Five\n2. Seven\n3. Nine")
else:
print("Something didn't go right!")
return StartSection()
CardsSection()
, :).