Python3 _ Stupid Methodology Python_ Diary _DAY4

Source: Internet
Author: User
Tags rewind

Day4

    • Exercise 19: Functions and variables
1 defcheese_and_crackers (Cheese_count, boxes_of_crackers):2     Print("You have %d cheeses!"%cheese_count)3     Print("You have %d boxes of crackers!"%boxes_of_crackers)4     Print("Man that's enough for a party!")5     Print("Get a blanket.\n")6 7 Print("We can just give the function numbers directly:")8Cheese_and_crackers (20,30)9 Ten Print("Or,we can use variables from our script:") OneAmount_of_cheese = 10 AAmount_of_crackers = 50 -  - cheese_and_crackers (Amount_of_cheese, amount_of_crackers) the  - Print("We can even do math inside too:") -Cheese_and_crackers (10+20, 5+6) -  + Print("and we can combine the variables and math:") -Cheese_and_crackers (Amount_of_cheese + 100, +Amount_of_crackers + 1000)

Results:

We can just give the function numbers directly:
You have a cheeses!
You have a boxes of crackers!
Man that's enough for a party!
Get a blanket.

Or,we can use variables from our script:
You have ten cheeses!
You have boxes of crackers!
Man that's enough for a party!
Get a blanket.

We can even do math inside too:
You have a cheeses!
You have one boxes of crackers!
Man that's enough for a party!
Get a blanket.

And we can combine the variables and math:
You have cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.

Make a function of yourself:

1 def Pingfang (x): 2     return x*x34print(Pingfang (5))56 y =pingfang (6)7print(y)

25
36

    • Exercise 20: Functions and files
1  fromSysImportargv2 3 #script name, parameter one (imported file)4Script, Input_file =argv5 6 defPrint_all (f):#define a new function: Print out all the files7     Print(F.read ())8 9 defRewind (f):#define a new function: Move the file pointer to the beginningTen f.seek (0) One  A defPrint_a_line (Line_Count, F):#define new function: Print (line number, contents of this line) -     Print(Line_Count, F.readline ()) -  theCurrent_file = open (input_file)#define variables to open the imported file as the current file -  - Print("First let ' s print the whole file:\n") -  + Print_all (current_file) -  + Print("Now let's rewind, kinds of like a tape.") A Rewind (Current_file) at  - Print("Let ' s print three lines:") -  -Current_file = 1 - print_a_line (Current_file, Current_file) -  inCurrent_file = current_file + 1 - print_a_line (Current_file, Current_file) to  +Current_file = current_file + 1 -Print_a_line (Current_file, Current_file)

    • Exercise 21: Functions can return something
1 defAdd (A, b):2     Print("ADDING%d +%d"%(A, b))3     returnA +b4 5 defSubtract (A, b):6     Print("subtracting%d-%d"%(A, b))7     returnAb8 9 defmultipy (A, b):Ten     Print("multiplaying%d *%d"%(A, b)) One     returnAb A  - defDivide (A, b): -     Print("dividing%d/%d"%(A, b)) the     returnAb -  - Print("Let's do some math with just functions!") -  +Age = Add (30, 5) -Height = Subtract (78,4) +Weight = multipy (90,2) AIQ = Divide (100,2) at  - Print("Age :%d, Height:%d, Weight:%d, IQ:%d"%(age, height, weight, IQ)) -  - #A Puzzle for the extra credits, type it in anyway. - Print("Here is a puzzle.") -  inwhat = Add (age, subtract (height, multipy (weight, divide (iq,2)))) -  to Print("That becomes:\n", What,"\ncan Do it by hand?")

Let's do some math with just functions!
ADDING 30 + 5
Subtracting 78-4
Multiplaying 90 * 2
Dividing 100/2
Age:35, height:74, weight:180, iq:50
Here is a puzzle.
Dividing 50/2
Multiplaying 180 * 25
Subtracting 74-4500
ADDING 35 +-4426
That becomes:
-4391.0
Can do it by hand?

Puzzle solving

1 def Jia (a,b,c,d,e): 2     Print (" the answer to the puzzle is all right. \ n%d + (%d-%d *%d/%d)" % (a,b,c,d,e)) 3     return A + (b-c*d/e)45 This=jia (35,74,180,50,2)6 Print (This) 7 Print (This==what)

The answer to the puzzle is all right
35 + (74-180 * 50/2)
-4391.0
True

Python3 _ Stupid Methodology Python_ Diary _DAY4

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.