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
7 Respuestas
+ 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
+ 2
def calc(x):
retrun(x*4,x**2)
side=int(input())
p,a = calc(side)
print("Perimeter: "+str(p))
print("Area: "+str(a))
+ 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))
0
OMG, thank you so much!
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)
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)
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