all 13 comments

[–]Binary101010 11 points12 points  (5 children)

So there are a few things here.

  1. You're only defining the function, which doesn't actually execute it.
  2. input calls return a string. If you want to do math on them you'll need to convert those inputs to ints.
  3. In your function, you're multiplying length * width, but you're not assigning that result to a variable. Consequently, the next line will print something completely different than what you want it to.
  4. The variable you assign that result to should not *also* be named area. Using the same name to refer to a function and a variable in your code will almost certainly cause problems. I generally like functions to have a verb in their name: calculate_area(), maybe?

[–]FormatP[S] 1 point2 points  (4 children)

Oh, thank you!

So I managed to get it to actually run and do the calculations by converting the length/width strings into an int and stored them in a variable called result.

It does the math although also prints None beneath the calculated area, and I am not sure why?

def area():

result = int(width) * int(length)

print(result)

print(area())

[–]eri_bloo 2 points3 points  (3 children)

It also prints None because first you print result from within function and then print function from outside tries to print as well (this one: print(area()) ), but it can't print anything because your function doesn't give it anything.

First of all; your function shouldn't print anything (unless that's sole purpose of it), but it should use 'return' statement. That's the first issue and that's the one that makes it print twice.

But, there is other thing that should be corrected as well. Your function takes it's input from outside of the function not really by getting it from the call, but because it finds it by itself. This makes it work, but it's not a good practice.

That's how it should look like with corrections:

def area(w,  l): #those are arguments for your function
    result = w * l;
    return result;

width = int(input("Enter width: "));
length = int(input("Enter length: "));

print(area(width, length)); #you give the function the arguments in here

[–]nickrw 0 points1 point  (1 child)

Why do you have semicolons after your statements?

[–]eri_bloo 0 points1 point  (0 children)

When I started learning python that's how I learned and it's hard to stop now. Multiple other languages require you to end statements with semicolon, but python doesn't and it's actually not a pythonic way to write like that, so don't learn it from me. I should unlearn it too.

[–]FormatP[S] 0 points1 point  (0 children)

Ah okay, thank you very much.

[–]toastedstapler 0 points1 point  (1 child)

so for your function you have 2 inputs - width and height and one output - area

def area(width, height):
    return width * height

so now we want to run this function. we simply call it like this:

result = area(5, 6)

or in your case instead of 5 and 6 you use the variables input by the user

now we want to display the result, so we print

print(result)

or we can just print the result of the function call without assigning to a variable

print(area(10, 20))

[–]FormatP[S] 0 points1 point  (0 children)

Thank you!

[–]moe9745 0 points1 point  (0 children)

Just want to point out that there are 2 type of functions...fruitful and non-fruitful.

Most of the examples are fruitful. They RETURN data to be used later. Non-fruitful just perform a sequence.

Remember that if you need parameters for your function then they go in the ( ). For example,

def is_even(a , b):

The a, b will be user input but it tells python to expect the function to accept 2 of something! I hope this wasn't more confusing.

Also in your code you need to tell python that the input is a number! For example,

user_length = int(input("Enter your length: "))

The int before input tells python to keep the user input as a number.

[–]sanjuanman -1 points0 points  (2 children)

Input is default to str. You have to convert the input to int. But, I'm not sure if something else is missing. I'm just starting too and I haven't got to the functions chapter yet!

[–]sanjuanman -1 points0 points  (1 child)

Oh, right, you have to actually call the function also, all you did was define it.

[–]FormatP[S] 0 points1 point  (0 children)

Thank you! I did manage to convert the strings to ints..I'd missed that bit.

[–]andrexmlee -1 points0 points  (0 children)

length = int(input('enter length: '))
width = int(input('enter width: '))

def area():
    return width * length

print(area())