Python learns the use of functions in 21:python, using functions for simple mathematical operations

Source: Internet
Author: User
Tags define function

Today I learned the usage of Python functions and how to define a function using Python.
And there are some minor errors in the code writing process, which is recorded to make it easy to find the point of the problem quickly when encountering the same error.

#--coding:utf-8--# defines 4 simple functions, namely add, subtract, multiply, divide, define function to use DEF this keyword def add (A, B): # Defines the Add function with the DEF keyword, assigns two parameters A and b print "A" to the Add function    Dding%d +%d "% (A, b) # prints out two variables in the function return a + b #利用return语句来返回函数的结果 # The description of the following 3 functions is the same as the Add function, do not repeat the def subtract (A, B): Print "Subtracting%d-%d"% (a, b) return a-bdef multiply (A, b): print "multiplying%d *%d"% (a, B) return A * bdef divide (A, b): print "Dividing%d/%d"% (a, b) return A/bprint "Let's do some math with just functions!" Age = Add (5) #使用add函数给age变量赋值, the resulting value is the result of two variables in the Add function after the return of the function height = subtract (4) Weight = Multiply (2) IQ = di  Vide (4, 2) print "Age:%d, Height:%d, Weight:%d, IQ:%d"% (age, Height, Weight, IQ) # A puzzle for the extra credits, type It in Anyway.print "This is a puzzle." The following formula is slightly more complex and should be read from the inside out to explain the formula: 1. The values of age, height, weight, and IQ are calculated by the function above respectively by 2. Then apply these values to the function to get the following mathematical expression 3. what = 35 + 74-180 * 50/24. The result is -4391 "what = Add (age, subtract (height, multiply (weight, divide (IQ, 2))) print" That BEcomes: "What is," Can do it by hand? " 

The results of the operation are as follows:

PS C:\Users\stephen\Desktop\python> python no21return.pyLet‘s do some math with just functions!ADDing 300 + 5SUBTRACTING 78 - 4MULTIPLYING 90 * 2DIVIDING 4 / 2Age: 305, Height: 74, Weight: 180, IQ: 2Here is a puzzle.DIVIDING 2 / 2MULTIPLYING 180 * 1SUBTRACTING 74 - 180ADDing 305 + -106That becomes:  199 Can you do it by hand?

There are exercises in the course that require normal methods to implement the same functions as what expressions, and I do not know if my understanding is correct, the following is a personal understanding: it is the use of simple mathematical expressions to accomplish what assignment.

age = 30+5height = 78-4weight = 90*2iq = 100/2print "age is: %d" % ageprint "height is: %d" % heightprint "weight is: %d" % weightprint "iq is: %d" % iqwhat = age + height - weight * iq / 2print "what = age+height-weight*iq/2: %d = %d + %d - %d * %d /2 " % (what, age, height, weight, iq )

The problems encountered in the code test are as follows:

    1. syntax error. I dropped a colon at the end of the function when I defined the Add function.

      PS C:\Users\stephen\Desktop\python> python .\No21return.pyFile ".\No21return.py", line 1def add(a,b)           ^SyntaxError: invalid syntax
    2. "Return" when the parameters are wrong. because of the use of notepad++, the software for a variety of languages can automatically pop up the language of the custom functions and the variables you have defined in the current script, the advantage is that you can speed up the writing of code, the disadvantage is that if you do not pay attention to automatically add unnecessary content, such as Python, you enter " D ", the built-in function on D will automatically show you a list. Notice the 7th line in my code, I accidentally input B into basestring when I "return a-a", so Python hints that the integer type cannot be mathematically operated with a function type and is not supported.
PS C:\Users\stephen\Desktop\python> python .\No21return.pyLet’s do some math with just functions!ADDing 30 + 5SUBTRACTING 78 - 4Traceback (most recent call last):  File ".\No21return.py", line 20, in <module>    height = subtract(78, 4)  File ".\No21return.py", line 7, in subtract    return a - basestringTypeError: unsupported operand type(s) for -: ‘int‘ and ‘type‘
    "%d" (formatted string) was omitted when
      print. The is given the value of the variable when it is printed, but print has no output.
        PS c:\users\stephen\desktop\python> python. \no21return-1.pyage is:35height is:74weight Is:180iq is:50t  Raceback (most recent): File ". \no21return-1.py", line, in <module>print "what = AGE+HEIGHT-WEIGHT*IQ/2: "% whattypeerror:not all arguments converted during string formatting  

      Same error, adding a formatted string to the code , because the format of output too much, careless missed out. In the 13th line of code, there should be 5 outputs, but only 4 are written in the print content.

        PS c:\users\stephen\desktop\python> python. \no21return-1.pyage is:35height is:74weight Is:180iq is : 50Traceback (most recent call last): File ". \no21return-1.py", line, in <module>print "what = age+height-weight* IQ/2:%d +%d-%d *%d/2 "% (age, height, weight, IQ, what) typeerror:not all arguments converted during string Formatt ing  

Tips: You can use the Cat command to view the contents of a document in Linux, and the same command in Windows PowerShell: " get-content ".

Get-Content,获取指定位置的项的内容。语法:Get-Content [-Path] <文件路径>[-Path]由方括号引起,表示可以写,也可以不写;不写则默认后面是文件路径,写了就指名道姓的说后面是文件路径。

Python learns the use of functions in 21:python, using functions for simple mathematical operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.