The Python loop 1

Source: Internet
Author: User

Cycle: While,for

For loop:

For I in Range (0,10,2):

Loop body

Note: Range inside the 2, is the step, that is, I value is: 0, 2,4,6,8; default is 1,for I in range (Ten) ==for I in range (0,10) ==for I in range (0,10,1)

End loop: Break: End Current Loop

Continue: Jump out of the loop and into the next loop

Write a loop: Guess the size of a number

Idea: Give a number to guess the size, when guessing the number is greater than the given number, return to guess the number is too large; When the guessed number is less than the given number, then return the guessed number is too small, when the guessed number equals the given number, then returns the success

The number of guesses is limited to 3 times, when the third guess is wrong, ask whether to continue guessing the word game, when the input is not ' n ' and ' n ', you can continue to guess three times.

good_number=57
Guess_count=0
While guess_count<3:
Guess_number=int (Input (' Please input your guess_number: '))
If Guess_number==good_number:
Print (' Good!!! You got it!!! ')
Break
Elif Guess_number<good_number:
Print (' Too bad!!! You guessed it is small. ')
Else
Print (' Too bad!!! You guessed it is big. ')

Guess_count+=1

If guess_count==3:
Print (' guessed wrong three times,do you want to keep guessing ... ')
Continue_game=input (' If you want-continue playing,you can enter any character or enter directly;\n '
"If you don't want to continue playing, please enter ' n '. \ nthe")
If continue_game!= ' n ' and continue_game!= ' n ':
Guess_count=0

The Python loop 1

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.