Python Forum
print within function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print within function (/thread-4375.html)



print within function - zpetsch - Aug-11-2017

I am writing a simple converter to change a bearing to a polar coordinate. I'm not quite sure what I'm doing wrong, but the code below won't print the value of the conversion before moving to anotherPolar():

def polar():
    direction = input("North or South?")
    angle = input("Angle from N Or S:")
    secondary = input("East or West?")
    polarangle = 0
    if direction == 'N' or direction == 'North':
        if secondary == 'E' or secondary == 'East':
            polarangle = round(90 - float(angle),4)
            return polarangle
        elif secondary == 'W' or secondary == 'West':
            polarangle = round(90 + float(angle),4)
            return polarangle
    elif direction == 'S'or direction == 'South':
        if secondary == 'E' or secondary == 'East':
            polarangle = round(270 + float(angle),4)
            return polarangle
        elif secondary == 'W' or secondary == 'West':
            polarangle = round(270 - float(angle),4)
            return polarangle
    print(polarangle)

polar()

def anotherPolar(): 
    again = input("Another?:")
    if again.lower() == "y" or again.lower() == "yes":
        polar()
        anotherPolar()
    elif again.lower() == "n" or again.lower() == "no":
        print("Goodbye")
    else:
        print("Invalid Entry")
        anotherPolar()

anotherPolar()



RE: print within function - wavic - Aug-11-2017

Hello! In polar() definition if the first if is passed the nested if statements have return statement. So if return is executed the function is not working enymore and the last line with the print(powarangle) doesn't execute.


RE: print within function - zpetsch - Aug-11-2017

Awesome, thank you so much!