BradTheBrutalitist - 1 year ago 55
Python Question

# Python: How do you make a variable = 1 and it still being that way in a different def block? (For a game)

I have a question. How do you get variables to stay defined in even when your in a different def functions? I have a function that serves a purpose of seeing how many

`AIs a user wants`
and for example if they choose 'One' for the amount of AIs they wanted
`AI1 would be equal 1`
. Here is where AI1 is defined:

``````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")
while(True):
#Same as line 4.
#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
``````

So if they
`choose 'One'`
only
`AI1 will be equal to 1`
. Then once this happens, The user will be able to select how many cards they want. This is based off of how many AIs they wanted activated So, in the previous paragraph
`One`
would be selected and so they will have the options of choosing from 3 different card amounts in the code below:

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

But when I ran the code It got to the
`def CardsSection`
(the section after the AISection where AI1 was definedin). Then gave me an error:

``````Traceback (most recent call last):
File "python", line 81, in <module>
File "python", line 68, in CardsSection
NameError: name 'AI1' is not defined
``````

The problem is I defined AI1 = 1 but my code wont recognize it. I think it is because it was defined in a different def. I thought if I used the global "Module" that whatever I put AI1, AI2 and AI3 equal to they would be like that regardless of what def Function I'm in. How can I make it so that wherever I set AI1, AI2, and AI3 equal to (In this case AISection), they will be that way regardless of where I am? Here is the full code i have so far:

``````def StartSection():#The starting section.
print("Do you want to play?\nChoices:\n1. Yes\n2. No")
while(True):
#Loops if user doesn't answer properly.
#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")
while(True):
#Same as line 4.
#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()
``````

I hope this helps :).

I believe you are facing a scope problem, with the fact that although you are defining it as global, you haven't defined it outside the function to begin with. If you define the three variables outside any of your functions, then your program appears to work:

``````def AISection():
global AI1, AI2, AI3
AI1 = 0
AI2 = 0
AI3 = 0
print("How many AIs do you want to play with?\nChoices:\n1. One\n2. Two\n3. Three")
while True:
if UserInput.lower() == "one":
AI1 += 1
break
``````

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

Also note, although this isn't code review, I'd highly suggest using UserInput.lower() == "one" instead of your list checking, it will be more efficient and accept more answers ("oNe" for example, if a user ever wanted to input that).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download