Darrell Shortt
Darrell Shortt

Reputation: 3

Program skipping sections its not supposed to

I have written a Home Siding Calculator program that I am including (I apologize for the length). When I run it I get no errors but it skips directly from Section I to Section VI to Section XI. It does not do any of the in-between parts. I do not understand why. Without the integer checks the program ran flawlessly.

print ("Siding Calculator")

CurrentWallNumber = 1
CurrentWinDoorNumber = 1
WallFactor = 0
WinDoorFactor = 0
GTotWallInches = 0
GTotWDInches = 0

#Section I: Number of walls input

def get_int(prompt): #WallFactor integer test
    while True:
        try:
            WallFactor = int(input("How many Walls? "))
            print("Number of Walls: ", WallFactor) 
            break
        except ValueError:
            print('Please enter a number! ')
    return WallFactor
for i in range(1):
    print(get_int('Enter a number: '))

#Section II: Current Wall number check

if int(CurrentWallNumber) <= int(WallFactor):   #Start CurrentWallNumber Loop
    print("Current Wall Number: ", CurrentWallNumber)

#Section III: Wall Height input & calculations

    def get_int(prompt):    #HeightFeet integer test
        while True:
            try:
                HeightFeet = int(input("How many feet high? "))
                break
            except ValueError:
                print("Please enter a number! ")
        return HeightFeet
    for i in range(1):
        print(get_int("Enter a number: "))

    def get_int(prompt):    #HeightInches integer test
        while True:
            try:      
                HeightInches = int(input("How many extra inches? "))
                TotHFtIn = int(HeightFeet) * 12  #Number of HeightFeet in inches
                TotHInches = int(TotHFtIn) + int(HeightInches)  #Total Height in inches
                break
            except ValueError:
                print("Please enter a number! ")
        return HeightInches
    for i in range(1):
        print(get_int("Enter a number: "))

#Section IV: Wall Width input & calculations

    def get_int(prompt):    #WidthFeet integer test
        while True:
            try:      
                WidthFeet = input("How many feet wide? ")
                break
            except ValueError:
                print("Please enter a number! ")
        return WidthFeet

    for i in range(1):
        print(get_int("Enter a number: "))

    def get_int(prompt):    #WidthInches integer test
        while True:
            try:      
                WidthInches = input("How many extra inches? ")
                TotWFtIn = int(WidthFeet) * 12  #Number of WidthFeet in inches
                TotWInches = int(TotWFtIn) + int(WidthInches)  #Total Width in inches
                TotWallInches = int(TotHInches) * int(TotWInches)  #Wall Area in inches
                WallArea = int(TotWallInches) / 144  #Wall Area in Square Feet
                print ("Wall Area: ", WallArea, "sqft")
                GTotWallInches = GTotWallInches + TotWallInches
                print("\n")              
                break
            except ValueError:
                print("Please enter a number! ")
        return WidthInches

    for i in range(1):
        print(get_int("Enter a number: "))

#Section V: Current Wall Number increment

CurrentWallNumber = CurrentWallNumber + 1   #End CurrentWallNumber loop

#Section VI: Number of Windows and Doors input

def get_int(prompt):    #WinDoorFactor integer test
    while True:
        try:
            WinDoorFactor = input("How many Windows & Doors? ")
            print("Number of Windows & Doors: ", WinDoorFactor)
            break
        except ValueError:
            print("Please enter a number! ")
    return WinDoorFactor
for i in range(1):
        print(get_int("Enter a number: "))

#Section VII: Current Windows & Doors number check

if int(CurrentWinDoorNumber) <= int(WinDoorFactor):     #Start CurrentWinDoorNumber loop
    print("Current Window/Door Number: ", CurrentWinDoorNumber)

#Section VIII:  WinDoor Height input & calculations

    def get_int(prompt):    #HeightFeet integer test
        while True:
            try:
                HeightFeet = input("How many feet high? ")
                break
            except ValueError:
                print("Please enter a number! ")
        return HeightFeet
    for i in range(1):
        print(get_int("Enter a number: "))

    def get_int(prompt):    #HeightInches integer test
        while True:
            try:            
                HeightInches = input("How many extra inches? ")
                TotHFtIn = int(HeightFeet) * 12  #Number of HeightFeet in inches
                TotHInches = int(TotHFtIn) + int(HeightInches)  #Total Height in inches
                break
            except ValueError:
                print("Please enter a number! ")
        return HeightInches
    for i in range(1):
        print(get_int("Enter a number: "))

#Section IX: WinDoor Width input & calculations

    def get_int(prompt):    #WidthFeet integer test
        while True:
            try:
                WidthFeet = input("How many feet wide? ")
                break
            except ValueError:
                print("Please enter a number! ")
        return WidthFeet
    for i in range(1):
        print(get_int("Enter a number: "))

    def get_int(prompt):    #WidthInches integer test
        while True:
            try:
                WidthInches = input("How many extra inches? ")
                TotWFtIn = int(WidthFeet) * 12  #Number of WidthFeet in inches
                TotWInches = int(TotWFtIn) + int(WidthInches)  #Total Width in inches

                TotWinDoorInches = int(TotHInches) * int(TotWInches)  #Window/Door Area in inches
                WinDoorArea = int(TotWinDoorInches) / 144  #Window/Door Area in feet
                print ("Window/Door Area: ", WinDoorArea, "sqft")
                GTotWDInches = GTotWDInches + TotWinDoorInches
                break
            except ValueError:
                print("Please enter a number! ")
        return WidthInches
    for i in range(1):
        print(get_int("Enter a number: "))

#Section X: CurrentWinDoorNumber increment

CurrentWinDoorNumber = CurrentWinDoorNumber + 1     #End of CurrentWinDoorNumber loop

#Section XI: Final Total

TotInches = int(GTotWallInches) - int(GTotWDInches)  #Total Wall inches - WinDoor inches
TotSqFt = int(TotInches) / 144  #Total Square Feet
print("Cumulative Area: ", TotSqFt, " SqFt")

Upvotes: 0

Views: 40

Answers (1)

Иван Балван
Иван Балван

Reputation: 598

The reason your program skips blocks 2,3 and 4 is because the value of the global variable WallFactor,that you have defined at the beginning of the script is still 0 when it appears in the if block in the second 'Section'. When you set WallFactor in the get_int function, you create a local variable, that has nothing to do with the global one. It lives only in that function. The fact that the get_int function returns WallFactor doesn't effect the global variable with the same name. You have to set that global variable WallFactor to the return value of the get_int function:

WallFactor=get_int(prompt)

Another way is to declare in the get_func function that the WallFactor is a global variable:

def get_int(prompt):
    global WallFactor
    ...

In the last case the return statement would be unnecessary.

The same thing happens with the global variable WinDoorFactor. Its corresponding get_int function does not change its value. There for when it appears in the following if block its value is 0.

Upvotes: 1

Related Questions