The introduction and use of the If of the 7th class Python

Source: Internet
Author: User

Today in Saturday, I rushed to Jinan, the road is far away, has been on the train, so did not come and update the course.


Today, to change back to the direct Python course, tomorrow to restore the reply number to see the status of the article. Everyone's homework yesterday did not do it, see a lot of small partners gave me, do better, also some students think should use the conditions to judge, everyone progress quickly, will answer the answer.

===================== =============================


Continue with the last program. We've been able to get the program to judge the value we've entered, but it's still a little bit, but it's going to have to say three times anyway. Because so far, our programs have been executed sequentially from top to bottom in a row. A classmate sent a question: how can it be based on the results of our input to choose the implementation? The answer is--if


Take a look at a picture (pure hand-painted, slag quality)

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/16/wKioL1dLrrvzUzdmAAAjo_HlPVg843.jpg-wh_500x0-wm_3 -wmp_4-s_828924546.jpg "title=" 0.jpg "alt=" Wkiol1dlrrvzuzdmaaajo_hlpvg843.jpg-wh_50 "/>

To explain, the sequence of the program is executed down when encountering the IF statement, it will judge the true and false conditions.

"If" is true, the next content is executed. "If" is false, skip.


The syntax is:


If condition:

SELECT statement to execute


Special note: The colon after the condition cannot be less, and must also be an English character.

Special NOTE: If internal statements need to have a uniform indentation, generally with 4 spaces. Python replaces {} In many other programming languages in this way. You can also choose 1/2/3 ... a space or press the TAB key, but it must be unified throughout the file. Do not use tab and space to mix, otherwise there will be a variety of inexplicable errors. So the recommendation is to use 4 spaces directly.


Give me a chestnut:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/16/wKioL1dLrtXAwhzYAAE9hb39sRI828.jpg-wh_500x0-wm_3 -wmp_4-s_1488236914.jpg "title=" 640.webp.jpg "alt=" Wkiol1dlrtxawhzyaae9hb39sri828.jpg-wh_50 "/>

Hisislove = input ()
If this was love:
Print "Then turn around and be brave to stay"


Try? Enter true and you will be answered. Enter false and nothing. (If you have problems exporting Chinese, please change to English)


So, our game can be rewritten like this:


num = 10
print ' Guess What I think? '
Answer = input ()

If Answer<num:
print ' Too small! '

If Answer>num:
print ' Too big! '

If Answer==num:
print ' bingo! '

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/82/16/wKioL1dLru-QeeDwAADo-Sq4nJk769.jpg-wh_500x0-wm_3 -wmp_4-s_2984025514.jpg "title=" 0 (1). jpg "alt=" wkiol1dlru-qeedwaado-sq4njk769.jpg-wh_50 "/>

If is referred to as a "control flow statement" in a programming language, it is used to control the order of execution. There are other control flow statements that we will use later.


Some children's shoes have found that the game is so boring there is no, because I know the answer, in to guess. It's like my left hand takes a stone and then I let go, with my right hand to catch just as uninteresting. Can that be further. Is there any way to let me not know the computer's answer, I guess again, this tomorrow to speak


People look at yesterday's homework, is there a new way to do it. Everyone is looking forward to the final shape of the game, in fact, I have for everyone to think of a good second game, oh, more than 100 lines of the program, everyone refueling it.


The introduction and use of the If of the 7th class Python

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.