全局变量及其值的问题

时间:2014-11-10 10:24:45

标签: python loops variables

我对此代码存在一些问题。还有很多其他代码可以使用它,但没有一个会干扰或影响我遇到的问题。所以基本上,当我运行代码并且我们到达底部的for循环时,它没有打印任何内容,因为显然是变量' wall' = 0,即使我已经给它一个有效的输入。如果有人能提供帮助,我将不胜感激。

global walls
global wallLengths
walls = 0
wall = 0
wallLengths = 0

def clientDetails():
    #global walls

    print("Welcome to the InHouse Solutions Room Painting Price Calculator")

    print("STEP 1 - CLIENT DETAILS")
    print("Please enter your full name")
    userName = input(">>>")

    print("Please enter your post code")
    postCode = input(">>>")

    print("Please enter you first address line here:")
    addressLineOne = input(">>>")

    print("Please enter your second address line here (OPTIONAL)")
    addressLineTwo = input(">>>")


    print("Thank you for your information")
    print (userName)
    print (addressLineOne + ", " + addressLineTwo + ", " + postCode)
    print (" ")

    def ValidationOne():
        print ("Is this information correct? Pleast enter Yes or No")
        clientDetailsCorrect = input(">>>")

        if clientDetailsCorrect == "no" or clientDetailsCorrect == "No":
            clientDetails()
        elif clientDetailsCorrect == "Yes" or clientDetailsCorrect == "yes":
            roomDimensions()
        else:
            ("Invalid response, please try again")
            ValidationOne()

    ValidationOne()      

def roomDimensions():
    global walls 
    print ("STEP 2 - ROOM DIMENSIONS")

    def ValidationTwo():
        global walls
        print ("How many walls does your room have?")
        walls = int(input(">>>"))
        if walls > 10 or walls < 3:
            print("Invalid, please enter a number between 3 and 10")
            ValidationTwo()
        elif walls == " " or walls == "":
            print("Invalid")
            ValidationTwo()

    def ValidationThree():
        global walls
        print ("How tall is the room in meters?")
        roomHeight = float(input(">>>"))
        if roomHeight < 2.4 or roomHeight > 6:
            print ("Invalid, please enter a value between 2.4 and 6")
            ValidationThree()

    def IndividualWalls():
        global wallLengths
        global walls
        for i in range(1,walls):
            print("Please enter the width of wall" , i)
            wallLengths[i] = float(input(">>>"))

    ValidationTwo()
    ValidationThree()
    IndividualWalls()
clientDetails()

2 个答案:

答案 0 :(得分:0)

没有必要使用&#39; global&#39;在脚本顶部声明全局时的关键字:

>>> walls = 0
>>> def increase_walls():
...   global walls
...   walls += 1
...   print walls
... 
>>> increase_walls()
1

答案 1 :(得分:-1)

我不能对你的问题发表评论,因为我的声誉不超过50,所以我会问这里 你能告诉我roomDimensions的功能吗? 我试图运行它,你有一些深度递归问题(你不能调用递归函数,没有任何if - 它将永远运行)但我注意到的第一件事是你没有初始化wall变量所以它不会是全局变量,它将是非局部变量。而且你没有调用任何内部函数(ValidationTwo,ValidationThree,IndividualWalls)

所以你的主要问题是:(按此顺序处理它们)

  1. 墙初始化
  2. 你没有调用任何内部函数
  3. 深度递归
  4. 这是我使用上述三个的例子:

    global_var = 12
    
    def outer():
        global global_var
        print("*"*10)
        print('hello im outer function')
        print("*"*10)
        def inner1():
            global global_var
            print('hello im inner1 function')
            if global_var < 10:
                return 'end for inner1' 
            print ('global_var is: ' + str(global_var))
            global_var -= 1
            return inner1()
    
        def inner2():
            global global_var
            print('hello im inner2 function')
            if global_var >= 10:
                return 'end for inner1' 
            print ('global_var is: ' + str(global_var))
            global_var += 1
            return inner2()
    
        if global_var >= 10:
            return inner1()
        else:
            return inner2()
    
    if __name__ == '__main__':
        print outer()