2016-05-29 3 views
4

Ich habe eine Frage. Wie erhalten Sie Variablen, um selbst dann definiert zu bleiben, wenn Sie in einem anderen Def-Modus arbeiten? Ich habe eine Funktion, die einen Zweck sieht, wie viele AIs a user wants und zum Beispiel, wenn sie "One" für die Menge der AIs wählen, die sie wollten AI1 would be equal 1. Hier ist, wo AI1 definiert:Python: Wie machst du eine Variable = 1 und es ist immer noch so in einem anderen Def-Block? (Für ein Spiel)

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 

Also, wenn sie choose 'One' nur AI1 will be equal to 1. Sobald dies geschieht, kann der Benutzer auswählen, wie viele Karten er haben möchte. Dieses basiert weg davon, wie viele KIs sie aktiviert, so wollte, im vorherigen Absatz One würde gewählt werden, und so werden sie die Optionen in der unten stehenden Code aus 3 verschiedenen Karten Mengen Auswahl:

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. 

Aber als ich lief den Code Es kam an die def CardsSection (der Abschnitt nach der AISection wo AI1 definiert wurde). Dann gab mir einen Fehler:

Das Problem ist, dass ich AI1 = 1 definiert, aber mein Code wird es nicht erkennen. Ich denke es liegt daran, dass es in einer anderen Definition definiert wurde. Ich dachte, wenn ich das globale "Modul" benutze, würde ich, egal was ich AI1, AI2 und AI3 gleichsetze, auch so sein, egal welche Funktion ich besitze. Wie kann ich es machen, egal wo ich AI1, AI2, und AI3 gleich (in diesem Fall AISection), werden sie so sein, egal wo ich bin? Hier ist der vollständige Code, den ich bis jetzt habe:

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

Ich hoffe, dass dies hilft :).

+1

Sie sollten den vollständigen Code oder mindestens genug davon einfügen, um das Problem zu reproduzieren. Aber versuchen Sie 'A1 = A2 = A3 = None' am oberen Rand Ihres Codes unter den Importen – Keatinge

+0

@Keatinge in Ordnung zu halten – BradTheBrutalitist

+0

@BradTheBrutalitist Ich habe eine sehr grundlegende Ansicht Ihres Codes rekonstruiert, löst das Ihr Problem? –

Antwort

4

Ich glaube, Sie stehen vor einem scope Problem, mit der Tatsache, dass, obwohl Sie es als global definieren, Sie es nicht außerhalb der Funktion zu Beginn definiert haben. Wenn Sie die drei Variablen außerhalb jeder Ihrer Funktionen definieren, dann wird Ihr Programm scheint zu funktionieren:

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: 
     UserInput = input("Answer goes here:") 
     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") 

Beachten Sie auch, obwohl dies nicht Code-Review ist, würde ich sehr empfehlen Userinput mit .lower() == "one" anstelle der Überprüfung Ihrer Liste, wird es effizienter und akzeptiert mehr Antworten ("oNe" zum Beispiel, wenn ein Benutzer das jemals eingeben wollte).

+0

Okay, ich danke Ihnen für das hilfreiche Feedback lassen Sie mich versuchen – BradTheBrutalitist

+0

Ich tatsächlich AI1 in der Funktion zu machen, machte ich AI1 + = 1 So wäre es gleich, aber dies half das Problem zu beheben – BradTheBrutalitist

+0

@BradTheBrutalitist Entschuldigungen für nicht zu verstehen, aber ich ' Ich bin froh, dass es geholfen hat, das zugrunde liegende Problem zu beheben :)! –

1

Das Problem ist, dass Sie die Variablen AI1, AI2 oder AI3 in Aisection() nicht definieren. Sie müssen sie dort auf 0 setzen, sonst sind sie nicht definiert.

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: 
     UserInput = input("Answer goes here:") 
     if UserInput.lower() == "one": 
      AI1 = 1 
      break 
+1

Das funktioniert auch, danke, dass Sie mir geholfen haben. :) – BradTheBrutalitist