Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input function
#11
The function you shared (without formatting) is incomplete.

def mytriangle (base, height):
    print ("The base is ", base)
    print ("The height is ", height)
    base (10)
    height (15)
    myarea = base * height / 2
Firstly, to use a function you have defined you have to call it. For example:

mytriangle(triangle1_base, triangle1_height)
Notice the variables used when calling the function do not need to be the same as the variable names used in the function definition. The latter are names internal to the function, that refer to the same objects referenced when the function is called (either explicitly, by providing literal numbers, or by using variables/expressions).

This means you already need to know the base and height information for a triangle you want to work out the area of because you need to supply that to the function. So you need code before you call the function to get that information.

You get data directly from the user using the input function which ALWAYS returns a reference to a str object, even if the the user only enters digits. In order to do maths on input information, you need to convert (cast) it to a number, usually either an integer (using int) or a floating point number (using float).

For example:

triangle1_base = float(input('Base size of first triangle? '))
I shall leave you to do the other line.

You could instead get your function to do the input. It is often better to separate the functions that do calculations from input and output. In the case of the latter, instead of printing anything, the function could simple return the answer (or the complete output including the answer) to be used elsewhere.

Inside your function you say base(10) but this is telling Python to call a function called base and pass a reference to the integer object 10 to it.
Reply
#12
It would be great if you followed the instructions buran gave you, specifically posting your code in code tags and posting the full traceback. We can't see what you're doing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,550 Feb-27-2020, 09:22 AM
Last Post: buran
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,814 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,512 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020