简体   繁体   中英

TypeError: areatriangle() takes 0 positional arguments but 2 were given

Im new to coding and im trying to learn python on coursera and im having issues with this one exercise

Exercise: Write a function def areatriangle(b, h): to compute the area of a triangle: formula is area =.5 * b * h . Output should look like: The area of a triangle of base 3 and height 5 is 7.5

You can test your function by executing the following code:

#%%
# The following will test areatriangle()
areatriangle(3,5)
areatriangle(2,20)
#%%

But whenever i try to run them in the console i get the error message

TypeError: areatriangle() takes 0 positional arguments but 2 were given

what should i do

TypeError: areatriangle() takes 0 positional arguments but 2 were given

This tells us that the areatriangle() method takes no input parameters.

areatriangle(3,5)

You have given two arguments (breadth and height). Now Python has no idea what to do with them and raises an error.

You can use this:

def areatriangle(breadth, height):
    area = .5 * breadth * height
    return area # or you can do: return f"The area of a triangle of base {breadth} and height {height} is {area}"


area = areatriangle(3, 5)
print(area)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM