+ 5

# Python3 Homework - calculate area

Hello guys, I am kinda new to Python3 and i study this subject at University. Now we got our first homework and although i have been listening to the lecturer and did many lessons of the Python course on sololearn i can't solve one excercise. The Task is to calculate the area of a Trapezoid with this formula: A= h*(a+b)/2 Therefor h=5 , a=(1,...,10) ,b=(2,...,11) is given. So we basically have all the variables but "a" and "b" shall vary from 1 to 10 and 2 to 11. Finally the code's Output should be the area of the Trapezoid (10x for each a and b). Maybe someone could help me with a code? :D I would be really grateful since i cant solve it myself. Chris

9 Answers

+ 14

# Christian
h = 5
a = range(1, 11)
b = range(2, 12)
def trapezoid():
for A in a:
for B in b:
print(h * (A + B) / 2)
trapezoid()

+ 15

Christian Since you already invested 3 hours... Â°_Â°
Glad to help! ^_^

+ 11

Sounds like 2 for-loops (nested)
like:
for a from x to y:
for b from x to y:
........... (pseudocode)
Give it a try by yourself. ^_^

+ 6

Christian Well, I wouldn't say you would need to go through the entire course as functions such as lambdas can be a little more of a complicated topic so they come later on in the course. Being a beginner, I would suggest just sticking with what you know and try solving the problem with that, but of course, going through the Python course will be very useful later on throughout your studies. d:

+ 5

So I don't want to give you the answer directly (given that this is a university project), but I may be able to help you through it.
Given that you would only need to calculate one expression, you have a few ways of doing so. The first way would be setting all of parameters as different pre set variables at the beginning of your code, where you can set a and b to be equal to the random function (https://www.sololearn.com/learn/Python/2438/). With the variables set in place, you can then just set the expression in a print statement with all of the variables to output the answer, using brackets to allow for the top portion (h*(a+b)) to go first (https://www.sololearn.com/learn/Python/2271/).
Another way that you could do this is by using lambdas (https://www.sololearn.com/learn/Python/2460/). Lambdas are basically of creating a function while not using the def statement and setting only a few things to happen. Within the lambda statement, you can have 4 different parameters (one for each variable in you equation), and set them to run in the equation above to find the area within the lambda.
Hope this helped! d:

+ 3

we ended our course at 12:00. I was so motivated to do my first homework since coding is such a interesting and exciting Topic for me... but now after 3 hours since i havent achieved anything i got really frustrated.
And when you see the solution you just think "damn that Looks so easy" :'D
At least you learn from such mistakes and i think i won't Forget that one :D

+ 2

i cant click the links --> runtime error... we just had our first lesson today :') and i am just getting started. Could it be because i am still in chapter 3? If so I guess i have to continue working myself through the chapters.
I didnt expect it to be that complicated, that I already have to work through all the Python lessons here on sololearn :O

+ 2

i just can't solve it by myself. It is just one of 5 Tasks we have to do till tomorrow. I already invested 3 hours in this one and i have no clue on how to solve it even though i read all those chapters about variables functions and methods. i just dont know what to do. I am pretty sure that there is a super simple solution since we had our first lesson today, but i just think in a difficult/wrong way.
Whatever thanks for trying to help. I don't understand your hints since the links don't work and I am not familiar with those wordings.
I guess I won't have this homework part then :'D

+ 2

# Valen.H
Wow you are my savior... When i see the code it is so simple. And i tried out so many shitty stuff. Honestly I have to say compared to JS and C++ , Python is really confusing when it comes to such Problems (for me).
Thanks for helping out! I will continue reading through chapters now ;) have a nice evening!