+ 1
Can Anyone tell me how to solve this? Project Name: Square up
Tuples can be used to output multiple values from a function. You need to make a function called calc(), that will take the side length of a square as its argument and return the perimeter and area using a tuple. The perimeter is the sum of all sides, while the area is the square of the side length. Sample Input: 3 Sample Output: Perimeter: 12 Area: 9
8 Answers
+ 5
Try this
def calc(x):
p=4*x
a=x*x
return (p,a)
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))
+ 4
Ok those answers might be technically correct, however the excercise was to achieve this through unpacking tuples, therefore here is the correct solution imo.
def calc(x):
#your code goes here
return (x*4, x*x)
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))
+ 2
def calc(x):
#your code goes here
return((4*x, x*x))
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))
+ 1
# you can solve it with any of these ways:
def calc(x):
sideLength = x
print(f"area = {sideLength ** 2}")
print(f"perimeter = {sideLength * 4}")
calc(int(input()))
calcs = lambda x : (x**2, x*4)
c1, c2 = calcs(int(input()))
print(f'area = {c1}', f'\nperimeter = {c2}')
def calc2(x):
return (x**2, x*4)
a, b = calcs(int(input()))
print(str(a) + ' is area', str(b) + ' is perimeter')
0
Just use the stmt,
return (peri, area)
While catching, use 2 variables like
res1, res2 = calc(4)
print("Perimeter :", res1)
print("Area:", res2)
Hope you know how to declare a function and the logic behind solving this prblm.
0
def calc(x):
#your code goes here
return side*4 , side**2
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))
0
def calc(x):
p=4*x
a=x*x
return (p,a)
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))
done...!
0
def calc(x):
#your code goes here
p = side+side+side+side
a = side**2
return p, a
side = int(input())
p, a = calc(side)
print("Perimeter: " + str(p))
print("Area: " + str(a))