Python guessing numbers game

Source: Internet
Author: User

Here to write a python guessing game of numbers.


#coding: Utf-8import randomreal_num = random.randrange (Ten) retry_count = 0while  Retry_count <= 2 :    guess_num = raw_input ("Please guess  the real num: "). Strip ()     if len (guess_num)  ==0:         continue    if guess_num.isdigit ()  :         guess_num = int (guess_num)     else:         print  "you need input a integer  Instead of string "    if guess_num > real_num:         print  "wrong! you need try smaller!"     elif guess_num < real_num:         print  "Wrong! you need try bigger! "     else:        print  "congratulations!  you got it! "         break    retry_count +=1else:     print  "The real num is", Real_num


/* Continue, to illustrate this script. Load a random module first.

Assigns a value of real_num to a number within 10.

Define an exit method if Retry_count =1 exits while loop executes the later else

Retry_count defines that the user can enter up to 3, because the system is calculated from 0.

Define a guess_num that allows the user to enter

Determine if the user input is not empty, if it is empty countinue

If the user enters a value that is not a number, it is converted to a number and tells the user to let him enter the number.

If the user enters a guess_num greater than or less than real_num, returns the corresponding print

Real_num at the top, we define this as a value within 10. Do you remember?

Guess_num the value entered for the user.

No greater than or less than, then the execution else tells the user that this is the number.

Loop here Retry_count +=1 the entire process executes 1 times and the user can perform up to 3 times.


In the end, if the user has finished 3 cycles and has not guessed, then the last else is executed, and the last else is at the outermost layer.

*/



To optimize it.

#coding: Utf-8import randomreal_num = random.randrange (Ten) Retry_count =0while retry_ Count <=2:        guess_num = int (Raw_input ("please  guess the real num: "))         if guess_num  > real_num:                 print  "wrong! your need try smaller!"         elif guess_num < real_num:                 print  "wrong!  your need try bigger! "         else:                 print  "you guessed it!"                 break        retry_count  +=1else:        print  "THE&NBSP;REAL&NBSP;NUM&NBSP;IS&NBSP;:", Real_num


This article from the "Clear Sky" blog, declined reprint!

Python guessing numbers game

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.