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