I tried a very simple function to calculate the rectangle area, but it doesn't work, it doesn't even get the input, anyone can see my code and let me know what is my problem in this code, please, and thank you?
def get_input(a,b):
a = int(input("Please enter the width: \n"))
b = int(input("Please enter the length: \n"))
def show_area(c):
print("The area of the rectangle is: {c}".format(c))
def calculate_area(a,b):
get_input(a,b)
c=a*b
show_area(c)
calculate_area(a,b)
CodePudding user response:
Your issue is scope; you're using the variables a
and b
but when you assign values to them within a function, because you haven't declared them global
, the assignment won't affect anything outside of that function. It also seems like you've declared a
and b
above somewhere, considering that you're executing calculate_area(a,b)
but haven't actually declared a
or b
in that scope in your example.
You also seem to think that if you pass in a
and b
as arguments to get_input
, that they will be affected in the original function. That's not how variables work in Python. If you pass an object, you can alter properties of that object when you pass the object to a function, but if you tried to change which object the variable was pointing to, that would only work if you declared a global
.
All you have to do is remove a
and b
from the arguments of your functions, and instead, return them from get_input
. Also, using f-strings makes the printing simpler:
def get_input(): # No need to pass in a and b; we're creating them here
a = int(input("Please enter the width: \n"))
b = int(input("Please enter the length: \n"))
return a, b # return a tuple of (a, b)
def show_area(c):
print(f"The area of the rectangle is: {c}") #f-strings are more straightforward
def calculate_area(): #no need to pass in a and b; they're created in the middle of this function
a, b = get_input() # you can assign multiple variables at once if the function returns a tuple
c = a * b
show_area(c)
calculate_area() # there is no a or b declared in this scope so no need to pass them in
If you had used globals instead, your code could look like this - you don't need to return anything, but you do have to declare a
and b
as globals if you want their value to change outside of the function:
# a and b must be defined within the module scope in order to use them within functions
a = 0
b = 0
def get_input():
global a
global b
a = int(input("Please enter the width: \n"))
b = int(input("Please enter the length: \n"))
def show_area(c):
print(f"The area of the rectangle is: {c}")
def calculate_area():
get_input()
c = a * b
show_area(c)
calculate_area()
Lastly, you could use a class to store a and b, so that you don't have to declare them as globals:
class AreaCalculator:
def __init__(self, a=0, b=0): # default a and b to zero
self.a, self.b = a, b
def get_input(self):
self.a = int(input("Please enter the width: \n"))
self.b = int(input("Please enter the length: \n"))
@staticmethod # this method is declared as "static" because it doesn't use any "self" variables
def show_area(c):
print(f"The area of the rectangle is: {c}")
def calculate_area(self):
self.get_input()
c = self.a * self.b
self.show_area(c)
ac = AreaCalculator()
ac.calculate_area()