ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Exercise: Number guessing game

2022-05-21 13:35:06  阅读:179  来源: 互联网

标签:guessing guess Number game user debug input hidden


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有