1
def getInt():
    while True:
        width = int(input("Enter width (1 - 60):"))
        if width > 1 and width < 60:
            break
        else:
            print("Please enter a valid input")
    while True:
        height = int(input("Enter width (1 - 20):"))
        if height > 1 and height < 20:
            break
        else:
            print("Please enter a valid input")
    return width, height


def calcPerimeter(width, height):
    perimeter = (2 * (width + height))
    return perimeter  

def calcArea(width, height):
    area = width * height
    return area


def main():
print('Results: {}'.format(getInt()))
print('The perimeter is {}' .format(calcPerimeter(width, height)))
print('The area is {}' .format(calcArea(width, height)))


main()

When I run, it says:

Traceback (most recent call last):

main()

print('The perimeter is {}' .format(calcPerimeter(width, height)))

NameError: name 'width' is not defined

3
  • Do you know variable scopes? Do you think width is visible in main method?
    – sagarr
    Oct 2, 2017 at 5:01
  • getInt() returns width and height, but when you call getInt(), you aren't saving the returned values anywhere. Try this: width, height = getInt(). Oct 2, 2017 at 5:01
  • If i print the values in getInt i get them back as the values, i just cant get them into the other functions. Oct 2, 2017 at 5:02

5 Answers 5

2

maybe you can try changing your main() into this

def main():
  width, height = getInt()
  print('Results: {}, {}'.format(width, height))  
  print('The perimeter is {}' .format(calcPerimeter(width, height)))
  print('The area is {}' .format(calcArea(width, height)))

you need to return getInt() to specific variable, before passing into another function

the other ways is calling calculatePerimeter and calculateArea inside the getInt() function, here is the example

def getInt():
    while True:
        width = int(input("Enter width (1 - 60):"))
        if width > 1 and width < 60:
            break
        else:
            print("Please enter a valid input")
    while True:
        height = int(input("Enter width (1 - 20):"))
        if height > 1 and height < 20:
        break
        else:
            print("Please enter a valid input")
    return calcPerimeter(width, height), calcArea(width, height)


def calcPerimeter(width, height):
    perimeter = (2 * (width + height))
    return perimeter  

def calcArea(width, height):
    area = width * height
    return area


def main():
  perimeter, area = getInt()
  print('Perimeter is {}, area is {}'.format(perimeter, area))  


main()
0

One way is to use Global Keyword. another way is to return the required variables from the function and saving them in the global scope.

0

You should read this post and in general study the rules of variable scopes. As a programmer this is essential knowledge.

Short Description of the Scoping Rules?

0

Refer this (link)[http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html] to understand why changing your main function to Po's answer makes the code run correctly.

The variables defined inside the functions calcPerimeter and calcArea are not visible/accessible to the main function because of being in a different scope.

0
#I think i have resolved your problem.In your case it was returning list/tuple .you have to convert it into simple variable
def getInt():
  while True:
     width = int(input("Enter width (1 - 60):"))
     if width > 1 and width < 60:
        break
     else:
        print("Please enter a valid input")
while True:
    height = int(input("Enter height (1 - 20):"))
    if height > 1 and height < 20:
        break
    else:
        print("Please enter a valid input")
return width,height


 def calcPerimeter(width, height):
   hw=width+height
   perimeter = 2*hw
   return perimeter  

def main():
  width,height= getInt()
  p=calcPerimeter(width, height)
  print('The perimeter is %s'%p)
  a=calcArea(width, height)
  print('The area is %s'%a)


main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.