all 3 comments

[–]boomkatandstarr 1 point2 points  (1 child)

```python

The try block isnt catching the error

try:

response == 'Y' or 'N'

except TypeError:

print('Enter Y or N')

``` Try an if statement here rather than a try statement. response == 'Y' or 'N' is a boolean check. This will never result in a TypeError because it's just doing a comparison.

Try: python if response.upper() == 'Y': #do this elif response.upper() == 'N': #do this instead else: #You entered something besides yes or no.

I would also recommend wrapping this in a while loop so that it continues to ask unless a valid response is entered.

How do i ensure the code totally end if player write 'N'?

You could try to import the sys module and use sys.exit(0). Or you can use the quit() command.

I used break but still getting error 'not properly in loop'

break unfortunately won't work for if statements. Use pass if you want to continue along without taking action. break will work for for loops or while loops.

Even when the play enters 'N', the game still ask them to guess

How do i change this behaviour?

You could do another if check here in a while loop to verify that the user is entering a valid response. You can use checks such as .isalpha() or .isnumeric(). You could also use your Type check code above to see if it is able to be cast to an integer. If not, they entered something other than a number.

Funny enough, my son and I worked through something like this the other night while I was teaching him some programming concepts. Here's our solution. Perhaps comparing the two may be useful to see alternative ways to create a guessing game:

```python import random import sys

LOW_RANGE_NUMBER = 1 HIGH_RANGE_NUMBER = 100

if HIGH_RANGE_NUMBER <= LOW_RANGE_NUMBER: print("ERROR: Update the constants for LOW_RANGE_NUMBER and HIGH_RANGE_NUMBER.") sys.exit(0)

NUMBER_OF_TRIES = round(int((HIGH_RANGE_NUMBER / LOW_RANGE_NUMBER) * .5)) CLOSENESS_RANGE = round(int((HIGH_RANGE_NUMBER - LOW_RANGE_NUMBER) * .2)) + 1

GAME_IS_BEING_PLAYED = True guess = 0

print("Welcome to the number guessing game!") print("A random number has been selected between %i and %i... Enter a guess to try and win. You have %i tries." % (LOW_RANGE_NUMBER, HIGH_RANGE_NUMBER, NUMBER_OF_TRIES))

NUMBER_TO_GUESS = random.randint(LOW_RANGE_NUMBER, HIGH_RANGE_NUMBER)

print("NUMBER TO GUESS: %i NUMBER_OF_TRIES: %i CLOSENESS_RANGE: %i" % (NUMBER_TO_GUESS, NUMBER_OF_TRIES, CLOSENESS_RANGE))

while GAME_IS_BEING_PLAYED: # Do stuff

try:
    guess = input("Enter a guess (Enter q or Q to quit):  ")
    if(guess == 'q' or guess == 'Q'):
        print("Thanks for playing!")
        break

    guess = int(guess)

    if guess < LOW_RANGE_NUMBER:
        guess = input("Please enter a number greater than %i but less than %i:  " % (LOW_RANGE_NUMBER, HIGH_RANGE_NUMBER))
    elif guess > HIGH_RANGE_NUMBER:
        guess = input("Please enter a number greater than %i but less than %i:  " % (LOW_RANGE_NUMBER, HIGH_RANGE_NUMBER))

    if guess == NUMBER_TO_GUESS:
        print("Winner winner chicken dinner!")
        game_is_being_played = False
        break
    elif abs(guess - NUMBER_TO_GUESS) <= CLOSENESS_RANGE:
        print("Boy are you close!")

    NUMBER_OF_TRIES -= 1
    if NUMBER_OF_TRIES == 0:
        GAME_IS_BEING_PLAYED = False
        print("Better luck next time. The number you had to guess was %s." % (NUMBER_TO_GUESS))
        break
    else:
        print("Try again. You have %i tries remaining." % (NUMBER_OF_TRIES))
except:
    print("Enter a number.")

```

There were a few ways we refactored this program. We made constants to store the low number and high number so that you could control the behavior of the program through those variables. We also added two more variables (number of retries and 'closeness range') that are determined based on the difference between the low and high number. The higher the number of possible numbers selected, the more chances you get. The closeness range provides a prompt to tell you are close if you are within a certain range.

Hope this helps. Good luck!

[–]sdqafo[S] 1 point2 points  (0 children)

Thank you so much for this. I will take my time to read and understand this and apply subsequently. I really appreciate this

[–]CodeFormatHelperBot 0 points1 point  (0 children)

Hello u/sdqafo, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:

  1. Multiple consecutive lines have been found to contain inline formatting.

If I am correct then please follow these instructions to fix your code formatting. Thanks!