其他分享
首页 > 其他分享> > Exercise: Number guessing game

Exercise: Number guessing game

作者:互联网

Exercise: Number guessing game

Level 0

• Using the random module the computer “thinks” about a whole number between 1 and 20.

• The user has to guess the number. After the user types in the guess the computer tells if this

was bigger or smaller than the number it generated, or if was the same.

• The game ends after just one guess.

Level 1

• The user can guess several times. The game ends when the user guessed the right number.

Level 2

• If the user hits ‘x’, we leave the game without guessing the number.

Level 3

• If the user presses ‘s’, show the hidden value (cheat)

Level 4

• Soon we’ll have a level in which the hidden value changes after each guess. In oredr to make

that mode easier to track and debug, first we would like to have a “debug mode”.

• If the user presses ‘d’ the game gets into “debug mode”: the system starts to show the current

number to guess every time, just before asking the user for new input.

• Pressing ‘d’ again turns off debug mode. (It is a toggle each press on “d” changes the value to

to the other possible value.)

Level 5

• The ‘m’ button is another toggle. It is called ‘move mode’. When it is ‘on’, the hidden number

changes a little bit after every step (+/-2). Pressing ‘m’ again will turn this feature off.

Level 6

• Let the user play several games.

• Pressing ‘n’ will skip this game and start a new one. Generates a new number to guess.

#Solution for Number Guessing (multi-game)
import random

debug = False
move = False
while True:
    print("\nWelcome to another Number Guessing game")
    hidden = random.randrange(1,201)
    while True:
        if debug:
            print("Debug:", hidden)

        if move:
            mv = random.randrange(-2, 3)
            hidden = hidden + mv

        user_input = input("Please enter your guess [x|s|d|m|n]: ") #add new option:'n(ext)'
        print(user_input)

        if user_input == 'x':
            print("Sad to see you leaving early")
            exit()

        if user_input == 's':
            print("The hidden value is", hidden)
            continue     

        if user_input == 'd':  #激活debug开关 if debug:生效
            debug = not debug  
            continue   

        if user_input =='m':  #激活debug开关 if move:生效
            move = not move
            continue

        if user_input =='n':  #选择n,退出当前(嵌套)while,返回上一级while.
            print("Giving up, eh?")
            break

        guess = int(user_input)
        if guess == hidden:
            print("Hit!")
            break

        if guess < hidden:
            print("Your guess is too low")
        else:
            print("Your guess is is too high")
guess the number
Please enter your guess [x for quit]: 100
100
Your guess is too low
Please enter your guess [x for quit]: 150
150
Your guess is is too high
Please enter your guess [x for quit]: 125
125
Your guess is too low
Please enter your guess [x for quit]: 138
138
Hit!
option switch
Please enter your guess [x|s|d]: s
s
The hidden value is 30
Please enter your guess [x|s|d]: d
d
Debug: 30
Please enter your guess [x|s|d]: x
x
Sad to see you leaving early

Welcome to another Number Guessing game
Please enter your guess [x|s|d|m|n]: n
n
Giving up, eh?

Welcome to another Number Guessing game

标签:guessing,guess,Number,game,user,debug,input,hidden
来源: https://www.cnblogs.com/clamber/p/16294921.html