Lesson 2: Designing the first game with Python

Source: Internet
Author: User

Directory:

First, a small game

Second, indentation

Third, BIF

Iv. 02 Lessons After class exercises and answers

*********************

First, a small game

*********************

#p2_1.py"""---First little game---"""Temp= Input ("Guess which number the turtle is thinking now:") Guess=Int (temp)ifGuess = = 8:      Print("lie down! Are you a worm in the heart of a little turtle? ")      Print("Well, there's no reward for guessing! ")Else:      Print("guess what, the little turtle is thinking about 8 now.")Print("game Over, no play ^_^")

Step1: Open Shell Open Idie

Step2: New Window Select the file->new window command (or you can just press CTRL + N to create a new file)

Step3: The typing code fills in the code according to the above format

STEP4: Save the file by pressing the shortcut key ctrl+s, save the source code as a file named p2_1.py

STEP5: Run the code input code to experience a bit,F5 Walk (you can also choose Run->run Module command)!

The results of the run execution are as follows:

Using the TAB key:

(1) Indent.

(2) After Idie to provide some suggestions, such as the input PR press the TAB key will display all possible commands for your reference.

Explanation of the program:

Python is not like c-like (all syntax is similar to the C language programming language), it is not difficult to find that in the above program there is no declaration of variables, and no curly braces, but with indentation in the expression. the "= =" is mostly to indicate whether the judgment is equal, and "=" indicates an assignment. User input data, input returns the data entered by the user to temp. There are no types of variables in Python, where input returns a string variable. The Next statement converts the data into an integer type. The next step is to judge the If-else statement, if the value of guess is 8, then execute the IF condition to indent inside the content.

***********

Second, indentation

***********

Indentation is the soul of Python. If you enter a colon (:) in the correct location, Idie automatically indents on the next line, just like the code, with a colon (:) behind the IF and else statements, and then press ENTER, and the code that starts with the second line is automatically indented.

**********

Third, BIF

**********

bif is the meaning of built-in Functions, built-in functions. For a few examples:

  print () is a built-in function that only needs to be called directly, and its function is "Print to the screen", which means to display the contents of the brackets on the screen.

  input () is also a BIF, which is used to receive user input and return it, in the code in front of the variable to receive the temp.

Python's variable is not required to be declared beforehand, directly to a valid name assignment, this variable is generated.

List of built-in functions

Enter Dir (_ _builtins_ _) in Idie to see a list of the built-in functions provided by Python. (In this case all the bif are in lowercase)

  

Help () This bif is used to display the function description of the BIF. For example: Help(print)

*******************************

Iv. 02 Lessons After class exercises and answers

*******************************

Test questions:

0. What is BIF?
1. How many BIF does the Python3 offer with the method of teaching the small turtle in class?
2. In Python's view: ' FISHC ' and ' FISHC '?
3. What is the most important thing about Python in the small turtle's eyes? Do you agree?
4. "=" and "= =" appear in the example of this lesson, they mean different meanings, do you accidentally write "=" in the process of programming? Is there a good way to solve this problem?
5. Have you ever heard of the word "splicing"?

Moving hands:

0. Write the program: hello.py, ask the user to enter a name and print "Hello, name!" ”
For example:


1. Write the program: calc.py requires the user to enter between 1 and 100 numbers and judge, the input conforms to the requirements of printing "Your sister is very beautiful", does not meet the requirements of printing "Your uncle is very ugly"
For example:

Answer:

Answer to test question:

0. What is BIF?

BIF is built-in Functions, built-in function. In order to facilitate the programmer to quickly write a script program (script is to program fast FAST!!!) Python provides a very rich built-in function that we just need to call directly, such as the function of print () is "printing to the screen", the Role of input () is to receive user input (Note: Python3 uses input () to replace the Python2 raw_input ( ), if you do not understand the use of the video to see the explanation.
1. How many BIF does the Python3 offer with the method of teaching the small turtle in class?

In Python or IDLE, enter Dir (__builtins__) to see a list of the built-in methods provided by Python (note that there are two underscores before and after builtins) where lowercase is BIF. If you want to see specific features of a BIF, such as input (), you can enter help (input) in the shell to get a description of the BIF function. Oh, the answer should be 68, don't trust yourself to count.

2. In Python's view: ' FISHC ' and ' FISHC '?

Not the same, because Python is a "sensitive little girl", so don't try to deceive her, for Python, FISHC and FISHC are completely different two names, so be sure to be careful when programming. But Python will help solve problems that might arise, such as when the identifier is already assigned (remember, the Turtle said in class that Python variables are not declared first) in order to be used in code, and the use of unassigned identifiers directly causes run-time errors. So you will soon be able to find this problem based on experience.

3. What is the most important thing about Python in the small turtle's eyes? Do you agree?

Zoom In! In the small turtle's eyes, indentation is the soul of Python, the strict requirements of indentation makes Python's code appear very thin and layered (the turtle has read a lot of Daniel's code, the Chaos ...). C language is not the international garbled contest well ... )。
So be very careful with indenting code in Python, and if you don't indent it correctly, what your code does may be a far cry from what you expect (as in the C language, the parentheses are in the wrong place).
If you enter the colon ":" in the correct location, IDLE will indent the next line automatically! 4. "=" and "= =" appear in the example of this lesson, they mean different meanings, do you accidentally write "=" in the process of programming? Is there a good way to solve this problem?
C language, if if (c = = 1) is written if (c = 1), the program is completely out of the programmer's original purpose to execute, but here in Python, sorry, not working, syntax error!Python does not allow assignment in the IF condition, so if C = 1: Error!

5. Have you ever heard of the word "splicing"?

In some programming languages, we can "add" two strings together, such as: ' I ' + ' love ' + ' FISHC ' will get ' ILOVEFISHC ', in Python, this practice is called splicing string.

Hands-On answers:

0. Write the program: hello.py, ask the user to enter a name and print "Hello, name!" ”

# hello.pyyourname = input ("Please enter your name:") print ("Hello," + yourName)

1. Write the program: calc.py requires the user to enter between 1 and 100 numbers and judge, the input conforms to the requirements of printing "Your sister is very beautiful", does not meet the requirements of printing "Your uncle is very ugly"

NUMBER0 = input (" Please enter a number from 1 to 100:"= int (NUMBER0)if# or so write if 0 <= number1 & number1 <=:      Print (" your sister is so beautiful Emm") Else :       Print (" your uncle is so ugly ~")

If 0 <= NUMBER0 <= 100: It's wrong to write!!!

Lesson 2: Designing the first game with Python

Related Article

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.