Randomly generate a number between 1 and 100 using the random variable
Collect the number entered by the user, if the input does not meet the requirements will let the user re-enter.
Enter meet requirements and the game starts. If the number is greater than the random number, the output number is too large; if it is less than the random number, the output number is too small
Guess right, the output number is correct, guess the number of times, and ask whether to continue the game
User answer Y (yes) means to continue playing
Import randomrnum=random.randint (1,100) count=0while true: num=input (' please enter a number (1,100): \n '). Strip () if num.isdigit (): num=int (num) count += 1 if num == rnum: print (' yes,{} is right;you guess {} times '. Format (num,count) ask=input (' Would you like play again (y/n): \n '). Strip (). Lower () if ask == ' y ': continue else: break break elif num > rnum: print (' you number is too lager! ') continue else: print (' You number is too small! ') continue else: print (' You number is invalid,please enter again ') continue
Python Guess small game