【问题标题】:Having troubles changing value of global variable无法更改全局变量的值
【发布时间】:2016-07-28 20:23:05
【问题描述】:

我正在尝试创建一个战舰游戏来练习我的编码,但是我无法更改全局变量的值。


turnsover = 0
diff = 0
ship_row = 0
ship_col = 0

def difficulty():

    global diff
    global turnsover
    diff = input("Please select a difficulty\n 1=Easy \n 2=Meduim \n 3=Hard \n 4=VS Machine\n")
    if diff.isdigit():
        diff = int(diff)

        if int(diff) not in range(1,5):
            print("Please select a correct difficulty level")
            difficulty()

        if diff == 1:
            turnsover == 20
            print("Difficulty level: Easy")

        if diff == 2:
            turnsover == 15
            print("Difficulty level: Meduim")

        if diff == 3:
            turnsover == 10
            print("Difficulty level: Hard")

        if diff == 4:
            turnsover == randint(1, 26)
            print("Difficulty level: Vs Machine")


        ####REMOVE AFTER PROD####
        print(turnsover)
    else:
        print("Please select a correct difficulty level")
        difficulty()

营业额的 prod 测试打印返回 0 而不是返回新的营业额(即剩余寿命)

【问题讨论】:

  • turnsover == 20 检查turnsover 是否等于20。
  • 如果你想练习,那就努力迈出下一步,成为一名优秀和负责任的开发人员,在没有global的情况下工作。

标签: python python-3.x


【解决方案1】:

正如@MorganThrapp 所说,您正在检查turnsoverturnsover == 10 等代码的相等性,而不是像这样为它分配值:turnsover = 0= 表示赋值== 表示平等。这应该很明显,因为您在代码开头为全局变量赋值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多