Python guess digital games

Source: Internet
Author: User

Python guess digital games
Here, we will write a python guess number game.

#coding:utf-8import randomreal_num = random.randrange(10)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. Enter the script. First load a random module. Assign real_num a value of less than 10. Define an exit method. If retry_count = 1, exit the while loop and execute the following else. retry_count defines that the user can enter up to 3, because the system starts from 0. Define a guess_num for user input to judge whether user input is null. If it is null, countinue is converted to a number if the value entered by the user is not a number, and tell the user to input a number. If the input guess_num is greater than or less than real_num, the corresponding printreal_num is returned at the top. We define a value less than 10. Remember that guess_num is the user input value. If the value is not greater than or less than the value, execute else to tell the user that this number is used. Here, retry_count + = 1 is executed once, and you can execute three times at most. At the end, if the user has finished three cycles and has not guessed it, execute the last else and the last else is in the outermost layer. */To optimize it.
#coding:utf-8import randomreal_num = random.randrange(10)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 real num is :",real_num

 

 

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.