Getting Started with Python basics-Implementing a Guess number game

Source: Internet
Author: User

Today, let's go through some of the previously learned points to complete a guess of the size of the game programming. Then, the general people write code straight up on the dry, no analysis, such a practice is not output, unless you are Daniel, today, I will learn programming ideas to share to everyone, my ideas may not be the best, but can give you a little inspiration, come, Let's take a look at this how to write this small program how to analyze it?

Requirements analysis: Using Python to achieve a guessing number of small games (scene can be self-prepared) Here I give the demand is, we still look at the flowchart first!

Program Analysis Flowchart:

Analysis Ideas:

1. If you set the default value that is the result number, then output "I" to initialize the settings, conversely, the system is randomly configured.

2. If the input number is equal to the random configuration number, return to congratulate you, guess right, program run end, if wrong more than 4 times, prompt wrong number of times, automatic exit program.

3. If there is no initialization value, then, random configuration, if guessed right, then return to congratulate you, guessed right, the program runs the end, if the wrong guess more than 4 times, the wrong number of guesses, automatic exit program

We step through the process of large code:

The first step: Create a variable to determine whether the user chooses a random configuration or a custom value.

Import Random #导包print"Start the game"Choose= str (Input ("enter I to set the integer, otherwise the system default number:"))ifChoose = ="I": #手动设置数值 secert=int(Input ("Please select an integer of 0-100:"))Else: Secert= Random.randint (0, -) #随机分配

Step two: Guess the game is bigger than the size

guess = Int (input ('Please enter the number you want to guess:')) Icount= 1 whileGuess! =Secert:ifGuess >secert:stemp= Input ("oops, the first"+ STR (icount) +'guess, a little bigger! \ n Please re-enter it:')    Else: Stemp= Input ("oops, the first"+ STR (icount) +'guess, a little bit! \ n Please re-enter it:') Guess=Int (stemp) icount= icount + 1ifIcount > 4:#mistake 4 times to exit the program        Print  "Section"+ STR (icount) +'times still not guessed it! I'm sorry, please start again.'quit ()Print "oops"+ STR (icount) +'guess right!, congratulations you find fighter.lu to red envelopes Oh! 'stemp= Input ('Press the ENTER key to exit the game!') quit ()

The complete code is as follows:

ImportRandomPrint "Start the game"Choose= str (Input ("enter I to set the integer, otherwise the system default number:"))ifChoose = ="I": Secert= Int (Input ("Please select an integer of 0-100:"))Else: Secert= Random.randint (0,100)Print 'guess number game start--------------->'Guess= Int (Input ('Please enter the number you want to guess:')) Icount= 1 whileGuess! =Secert:ifGuess >secert:stemp= Input ("oops, the first"+ STR (icount) +'guess, a little bigger! \ n Please re-enter it:')    Else: Stemp= Input ("oops, the first"+ STR (icount) +'guess, a little bit! \ n Please re-enter it:') Guess=Int (stemp) icount= icount + 1ifIcount > 4:        Print  "Section"+ STR (icount) +'times still not guessed it! I'm sorry, please start again.'quit ()Print "oops"+ STR (icount) +'guess right!, congratulations you find fighter.lu to red envelopes Oh! 'stemp= Input ('Press the ENTER key to exit the game!') quit ()

Here, we guess the game program is done.

Getting Started with Python basics-Implementing a Guess number game

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.