Philip_99
Philip_99

Reputation: 15

While loop won't stop looping

I'm kind of new to python or coding in general and I have run into a problem in my while-loop. I want to point out that the while-loop only works when the variable "loop" = True

loop = True #Makes the asking sequence work


def loop_again(): #Makes the program ask if you want to continue
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again

When I write "No" when the program asks me if I want to do this again, it still loops the sequence, even though the variable "loop" is supposed to be false when i type "No" which is supposed to stop the loop.

Full code (while loop at the bottom of the code):

#Solving for the area in a shape automatically

import math

loop = True #Makes the asking sequence work


def loop_again(): #Makes the program ask if you want to continue
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again



def sqr_area(): #Asking sequence for the area of the square
    if choose == "Square":
        a = float(input("Input the length of the side here: "))
        print(a ** 2)
        loop_again()

def rec_area(): #Asking sequence for the area of the rectangle
    if choose == "Rectangle":
        a = float(input("Input the length of the long sides here: "))
        b = float(input("Input the length of the short sides here: "))
        print(a * b)
        loop_again()

def tri_area(): #Asking sequence for the area of the triangle
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)
    loop_again()

def cir_area(): #Asking sequence for the area of the circle
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)
    loop_again()

while loop == True: #While loop, asking sequence
    choose = str(input("Input what shape that you want to figure out the area of here: "))
    if choose == "Square":
        sqr_area()
    elif choose == "Rectangle":
        rec_area()
    elif choose == "Triangle":
        tri_area()
    elif choose == "Circle":
        cir_area()
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        choose

Thanks in advance!

Upvotes: 1

Views: 632

Answers (3)

Guinther Kovalski
Guinther Kovalski

Reputation: 1929

It's a context variable problem. The loop variable you have in loop_again() and the one inside the while loop are different ones. Every variable inside a function, in python, is local variable, unless its an argument, or if you use global variable in the main and in inside the functions.

So make it global or pass and return it inside the functions

#Solving for the area in a shape automatically

import math
global loop 
loop = True #Makes the asking sequence work


def loop_again(): #Makes the program ask if you want to continue
    global loop 
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again



def sqr_area(): #Asking sequence for the area of the square
    if choose == "Square":
        a = float(input("Input the length of the side here: "))
        print(a ** 2)
        loop_again()

def rec_area(): #Asking sequence for the area of the rectangle
    if choose == "Rectangle":
        a = float(input("Input the length of the long sides here: "))
        b = float(input("Input the length of the short sides here: "))
        print(a * b)
        loop_again()

def tri_area(): #Asking sequence for the area of the triangle
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)
    loop_again()

def cir_area(): #Asking sequence for the area of the circle
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)
    loop_again()

while loop == True: #While loop, asking sequence
    global loop 
    choose = str(input("Input what shape that you want to figure out the area of here: "))
    if choose == "Square":
        sqr_area()
    elif choose == "Rectangle":
        rec_area()
    elif choose == "Triangle":
        tri_area()
    elif choose == "Circle":
        cir_area()
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        choose

you need to say in every function that loop is the global one, otherwise python you interpret it as a local variable.

the other way is:

import math
loop = True #Makes the asking sequence work


def loop_again(loop ): #Makes the program ask if you want to continue
    loop_again = str(input("Do you want to do this again? Yes or No: "))
    if loop_again == "Yes":
        loop = True
    elif loop_again == "No":
        loop = False
    else:
        print("Please answer yes or no: ")
        loop_again
    return loop



def sqr_area(loop): #Asking sequence for the area of the square
    if choose == "Square":
        a = float(input("Input the length of the side here: "))
        print(a ** 2)
        loop = loop_again()
    return loop

def rec_area(loop): #Asking sequence for the area of the rectangle
    if choose == "Rectangle":
        a = float(input("Input the length of the long sides here: "))
        b = float(input("Input the length of the short sides here: "))
        print(a * b)
        loop = loop_again(loop)
    return loop


def tri_area(loop): #Asking sequence for the area of the triangle
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)
    loop = loop_again()

def cir_area(loop): #Asking sequence for the area of the circle
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)
    loop = loop_again()

while loop == True: #While loop, asking sequence
    choose = str(input("Input what shape that you want to figure out the area of here: "))
    if choose == "Square":
        loop = sqr_area(loop )
    elif choose == "Rectangle":
        loop = rec_area(loop )
    elif choose == "Triangle":
        loop = tri_area(loop )
    elif choose == "Circle":
        loop = cir_area(loop )
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        choose```

Upvotes: 0

chepner
chepner

Reputation: 532208

Don't use recursion when you should be using a loop, and loop_again should return a value instead of setting loop globally.

import math


# Returns true once the input is Yes or false once the input is No
def loop_again():
    while True:
        response = str(input("Do you want to do this again? Yes or No: "))
        if response == "Yes":
            return True
        elif response == "No":
            return False
        else:
            print("Please answer yes or no: ")

loop_again should be called after the relevant *_area function has returned, not inside each function. The functions don't need to know or care about the value of choose; they are only called when they are intended to be called.

# Print the area of a square
def sqr_area():
    a = float(input("Input the length of the side here: "))
    print(a ** 2)


# Print the area of a rectangle
def rec_area():
    a = float(input("Input the length of the long sides here: "))
    b = float(input("Input the length of the short sides here: "))
    print(a * b)


# Print the area of a triangle
def tri_area():
    a = float(input("Input the length of the side: "))
    b = float(input("Input the length of the height: "))
    print((a * b) / 2)


# Print the area of a circle
def cir_area():
    r = float(input("Length of the radius: "))
    print((r ** 2) * math.pi)

The final loop can run indefinitely, until loop_again returns True.

# Loop until the user chooses to not run again
while True:
    choose = input("Input what shape that you want to figure out the area of here: ")
    if choose == "Square":
        sqr_area()
    elif choose == "Rectangle":
        rec_area()
    elif choose == "Triangle":
        tri_area()
    elif choose == "Circle":
        cir_area()
    else:
        print("Invalid shape, Input one of these shapes: Square, Rectangle, Triangle, Circle")
        continue

    if loop_again():
        break

Upvotes: 2

Óscar López
Óscar López

Reputation: 236122

In the loop_again() function you have to add this at the beginning:

global loop

Otherwise, the variable is considered local and won't have any effect on the other loop variable that's in the outer scope.

Upvotes: 1

Related Questions