Having troubles changing value of global variable

时间:2016-07-28 20:23:06

标签: python python-3.x

I am trying to create a battleships game to practice my coding, however I am having trouble changing the value of a global variable.


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

The prod test print of turnsover returns 0 instead of returning the new amount of turnsover (aka lifes remaining)

1 个答案:

答案 0 :(得分:0)

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