#Guess number GameImportRandomdefNumgame (self): Total=0 while1: #because IsDigit is judged below, NUM is treated as a str type, and num on the back requires a separate int, which does not take effect if the line int (num) is on totalnum = input ("Please enter the number in 0-999, enter-1 end:") ifnum = ="-1": Break elifNum.isdigit () and0 <= Int (num) < 1000: Num=int (num) total+ = 1Print("you have effectively entered%d times"%Total ) Random_num= Random.randrange (0,1000) ifnum >Random_num:Print("the number you entered is larger than the program random number, and the program random number is%d."%random_num)elifnum = =Random_num:Print("you guessed it, the program random number is%d."%random_num)Else: Print("the number you entered is smaller than the program random number, and the program random number is%d."%random_num)Else: Print("input wrong, please re-enter! ")if __name__=="__main__": Numgame (0)
Define Guess numbers game function