A simple guess number game
The first two days in the forum to answer questions, see a guess the number of games, on the original basis changed a bit, play a play.
This program, the number range and the number of attempts is predetermined, of course, can be modified by the code. After testing, because of the difficulty, I added the cheat function, smart you will be able to find out ~
Code ideas:
Randomly generated initial number random.randint ()
Number of cycle limit attempts
Compares the number of inputs to the initial number and outputs the comparison results
After a constant attempt, the equivalent will download a violent image, the image of the violent diffuse two
Imglist = Re.findall ('data-original-image-url= "(. *?\.jpg)"'= Re.findall (' src= "(https://wanzao2.b0.upaiyun.com/web_maker/.*?-picsmall)', HTML) # Another is a series of photos, Because of a series of this I have not thought how to distinguish between different photos, so it makes the last one
Finally, using Urllib to download a random list, take this picture as a reward for the right answer. Actually can do the page turn, but think is reward, not too much necessary.
Code:
ImportReImportRequestsImportRandomImporturllib.requestdefgethtml (URL): Headers= { 'user-agent':'mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) gecko/20100101 firefox/57.0', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'accept-encoding':'gzip, deflate', 'upgrade-insecure-requests':'1',} page= Requests.get (url,headers=headers) HTML=Page.textreturnHTMLdefgetimg (HTML): Imglist= Re.findall ('data-original-image-url= "(. *?\.jpg)"', HTML)#. Decode (' Utf-8 ') #Picsmall = Re.findall (' src= ' (https://wanzao2.b0.upaiyun.com/web_maker/.*?-picsmall) ', HTML)rad =Random.randint (0,len (imglist)) Urllib.request.urlretrieve (Imglist[rad],'happy moment. jpg') Print('Please view today's pictures in the catalogue ...')if __name__=='__main__': I=1Jihui= 8 Number=random.randint (1,1000) Usage=" "-------------------------Daily Smile v 1.0--number range: 1-1000--Number of attempts: 8----------------- -------- " " Print(usage) Temp=int (Input ('guess what the numbers are today:')) whileI<9: iftemp = = 123456: Print('the answer is'+ STR (number) +'...') iftemp!=Number:jihui-= 1ifJihui = =0:Print('The opportunity ran out ...') Break ifTemp>Number :Print('Oh, big! '+'still remaining'+ STR (Jihui) +'Second Chance.') eliftemp<Number :Print('Oh, small, small! '+'still remaining'+ STR (Jihui) +'Second Chance.') Temp=int (Input ('Please guess again:')) Else: Print('Yes, right!!! ') HTML= Gethtml ("http://baozoumanhua.com/all/hot?page=1") getimg (HTML) BreakI+=1Print('Game Over ~')
"Python little Exercise" simple guess number game