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