Python:如何使变量= 1,并且在其他def块中仍然如此?(对于游戏)

布拉德·布鲁塔利特主义者

我有个问题。即使在不同的def函数中,如何使变量保持定义状态?我有一个功能,目的是查看有多少个AIs a user wants,例如,如果他们为所需的AI数量选择了“一个” 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'只是AI1 will be equal to 1然后,一旦发生这种情况,用户将能够选择他们想要多少张卡。这是基于要激活多少个AI的,因此,在上一段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(在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 Function中,我都将如何设置它,以便无论在何处设置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()

我希望这有帮助 :)。

亚历山大·克拉格斯

我相信您面临一个范围问题,尽管您将其定义为全局的,但您并没有在函数的开头定义它。如果您在任何函数之外定义了三个变量,则您的程序似乎可以正常工作:

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

还要注意,尽管这不是代码审查,但我还是强烈建议您使用UserInput.lower()==“ one”代替列表检查,这样会更有效并接受更多答案(例如,“ oNe”用户曾经想输入的内容)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python:如何使变量= 1,并且在其他def块中仍然如此?(对于游戏)

来自分类Dev

如何允许从Ruby中的特定类初始化一个类,并且在其他地方没有?

来自分类Dev

Python:如何在其他函数中使用一个函数中的命名变量

来自分类Dev

局部变量-如何在其他if-sling中访问(python)

来自分类Dev

在python中,如何测试可能是其他类型的变量?

来自分类Dev

Python-如何从其他文件导入函数中的变量?

来自分类Dev

如何从python中的其他函数访问变量

来自分类Dev

关于python中变量的困惑。python如何使用变量?

来自分类Dev

查找在其他模块中定义的变量(python)

来自分类Dev

Python Tkinter变量未在其他线程中更新

来自分类Dev

查找在其他模块中定义的变量(python)

来自分类Dev

如何在python类中的其他函数中定义(变量)的函数内使用变量?

来自分类Dev

我们如何在python的其他变量中存储某个变量的先前值?

来自分类Dev

Python如何知道类中的变量是方法还是变量?

来自分类Dev

如何在python中将变量放入变量中

来自分类Dev

实例变量如何与python中的类变量相关

来自分类Dev

J2ME-如何使线程返回值,并且在该线程完成后,在其他操作中使用返回值?

来自分类Dev

如何在 Python 中的 def __init__ 中检索变量

来自分类Dev

如何从python中的函数中获取变量

来自分类Dev

如何自动清除python中的变量?

来自分类Dev

如何在Python中动态引用变量

来自分类Dev

如何从python中的instancetype变量获取数据

来自分类Dev

如何检查python中变量的分布?

来自分类Dev

变量如何在Python中工作?

来自分类Dev

如何在python中定义空变量

来自分类Dev

如何在python中测试变量?

来自分类Dev

Pygame Python如何从变量中绘制矩形

来自分类Dev

如何确定Python中设置变量的大小?

来自分类Dev

如何在Python中创建变量代码?

Related 相关文章

  1. 1

    Python:如何使变量= 1,并且在其他def块中仍然如此?(对于游戏)

  2. 2

    如何允许从Ruby中的特定类初始化一个类,并且在其他地方没有?

  3. 3

    Python:如何在其他函数中使用一个函数中的命名变量

  4. 4

    局部变量-如何在其他if-sling中访问(python)

  5. 5

    在python中,如何测试可能是其他类型的变量?

  6. 6

    Python-如何从其他文件导入函数中的变量?

  7. 7

    如何从python中的其他函数访问变量

  8. 8

    关于python中变量的困惑。python如何使用变量?

  9. 9

    查找在其他模块中定义的变量(python)

  10. 10

    Python Tkinter变量未在其他线程中更新

  11. 11

    查找在其他模块中定义的变量(python)

  12. 12

    如何在python类中的其他函数中定义(变量)的函数内使用变量?

  13. 13

    我们如何在python的其他变量中存储某个变量的先前值?

  14. 14

    Python如何知道类中的变量是方法还是变量?

  15. 15

    如何在python中将变量放入变量中

  16. 16

    实例变量如何与python中的类变量相关

  17. 17

    J2ME-如何使线程返回值,并且在该线程完成后,在其他操作中使用返回值?

  18. 18

    如何在 Python 中的 def __init__ 中检索变量

  19. 19

    如何从python中的函数中获取变量

  20. 20

    如何自动清除python中的变量?

  21. 21

    如何在Python中动态引用变量

  22. 22

    如何从python中的instancetype变量获取数据

  23. 23

    如何检查python中变量的分布?

  24. 24

    变量如何在Python中工作?

  25. 25

    如何在python中定义空变量

  26. 26

    如何在python中测试变量?

  27. 27

    Pygame Python如何从变量中绘制矩形

  28. 28

    如何确定Python中设置变量的大小?

  29. 29

    如何在Python中创建变量代码?

热门标签

归档