0

Tuple Unpacking

Hey, guys. I'm having trouble completing the task. Help please. this is task: 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 But i have not idea that i need to do. I have this: def calc(x): Perimeter = x*4 Area = x**2 return Perimeter return Area side = int(input()) p, a = calc(side) print("Perimeter: " + str(p)) print("Area: " + str(a)) If you know how do it? please help:3

7th Jun 2021, 11:44 AM
Andrey Zinin
7 Answers
+ 11
First see that what is a tuple? A Tuple is a collection of Python objects separated by commas. In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable. Credit - geeks for geeks You have to make your program like this - def calc(x): return (x*4 , x**2) side = int(input()) p, a = calc(side) print("Perimeter -",p) print("Area -",a) Code - https://code.sololearn.com/c5dcvKJ15AwK/?ref=app
7th Jun 2021, 11:51 AM
Abhiyantā
Abhiyantā - avatar
+ 2
def calc(x): retrun(x*4,x**2) side=int(input()) p,a = calc(side) print("Perimeter: "+str(p)) print("Area: "+str(a))
10th Aug 2022, 3:58 PM
Abdul Ghafar
Abdul Ghafar - avatar
+ 1
My Answer: 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))
10th Nov 2022, 8:18 AM
Pooja Patel
Pooja Patel - avatar
0
OMG, thank you so much!
7th Jun 2021, 12:01 PM
Andrey Zinin
0
A Tuple is a collection of Python objects separated by commas. In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable. You have to make your program like this - def calc(x): return (x*4 , x*2) side = int(input()) p, a = calc(side) print("Perimeter -",p) print("Area -",a)
9th Oct 2021, 12:58 PM
Mahesh Devalla
Mahesh Devalla - avatar
0
but where did the 4 and 2 come from or what is their representation? how did you come to the conclusion to use return (x*4 , x*2)
27th Dec 2021, 2:18 PM
Devin Robinson
0
In this case, • perimeter (p) is the sum of all sides, in other words: p = side*4 • area (a) is the square of the side length: a = side**2
5th Aug 2022, 3:11 AM
Syahril Dimas Sabirin
Syahril Dimas Sabirin - avatar