Python implementation guess number game

Source: Internet
Author: User

Game rules:

1. Randomly generate integers between 1 and 100
2. A total of 3 guesses, each guessing if incorrect will prompt is greater than or less than the target value, 3 times the opportunity to run out of the program
3. Three opportunities, including 3rd chance if guessed, prompt the user to guess
4. If the user input is not legal need to be prompted and re-enter

The code is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2017-09-03 16:33#@Author: Aixce#@Email: [Email protected]#@File: charades. PY#@Software: PycharmImportRandomPrint("######## guess number games #########")Print("# 1. Start the Game 2. Exit the game with any key #")Print("# Note: Guess the wrong three times the game automatically exits. #")Print("##################################") put= Input ("Please enter:")ifput = ="1": number= 3Secret= Random.randint (1, 100)     whileNumber >0:temp= Input ("guess what I'm thinking now is the number:") Temp2=Temp.strip ()iftemp2.isdigit (): Temp1=Int (TEMP2)ifTemp1 = =Secret:Print("hum, I think of the number is%s, you actually guessed, guess also no reward ^_^"%secret)elifTemp1 >Secret:Print("I thought the number is smaller than%s, or the remaining%s chance"% (temp1,number-1))            Else:                Print("I thought about the number larger than%s, or the remaining%s chance"% (temp1,number-1))        Else:            Print("Error: '%s ' is not a number, please enter an integer"%temp) number+ = 1 Number-= 1Else: Exit ("exit the game successfully!")Print("Game Over!")

Execution Result:

Python implementation guess number game

Related Article

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.