2
\$\begingroup\$

I know this is kind of stuff is somewhat done to death but please still try to review it fairly.

My code will only go within the interval of [1,9]

def guessing_game():
    no_of_guess = 0
    c = "y"
    import random
    a = random.randint(1,9)
    while c == "y":
        b = input("Enter a guess: ")
        if b.isdigit():
            if int(b) == a:
                c = input("You got it! Play again? Enter \"Y\" if yes, and anything else to exit. ").lower()
                no_of_guess +=1
                a = random.randint(1,9)
            elif int(b) > a:
                c = input("Too high. Try again? Enter \"Y\" if yes, and anything else to exit. ").lower()
                no_of_guess +=1
            else:
                c = input("Too small. Try again? Enter \"Y\" if yes, and anything else to exit. ").lower()
                no_of_guess +=1 
        else:
            c = input("Haha. Try again? Enter \"Y\" if yes, and anything else to exit. ").lower()
    if c != "y":
        print("How unfortunate. Well, you made %s guess(es)."%no_of_guess)


guessing_game()
\$\endgroup\$
2
\$\begingroup\$

A few minor things:

  • Imports should generally be at the module level, not inside functions.

  • Use more intention-revealing variable names. no_of_guess is borderline OK, but a, b and c don't tell me anything.

  • Think about control flow more carefully. For example:

    • you increment the guess counter for any numerical input, so you don't necessarily need to wait for the conditional checks; and

    • outside the loop do you really need to recheck whether the input wasn't "y"?

  • You can use single-quoted strings to avoid escaping double quote within them.

  • You can also reduce duplication by extracting functions and constants

With these suggestions addressed:

import random

RULES = 'Enter "Y" if yes, and anything else to exit. '

def win_prompt():
    return input("You got it! Play again? " + RULES).lower()

def lose_prompt(message):
    return input(message + " Try again? " + RULES).lower()

def guessing_game():
    number_of_guesses = 0
    target = random.randint(1,9)
    continuing = "y"
    while continuing == "y":
        guess = input("Enter a guess: ")
        if guess.isdigit():
            number_of_guesses += 1
            if int(guess) == target:
                continuing = win_prompt()               
                target = random.randint(1,9)
            elif int(guess) > target:
                continuing = lose_prompt("Too high.")
            else:
                continuing = lose_prompt("Too small.") 
        else:
            continuing = lose_prompt("Haha.")
    print("How unfortunate. Well, you made %s guess(es)." % number_of_guesses)


guessing_game()
\$\endgroup\$
  • \$\begingroup\$ Thanks a bunch! This helps a lot, especially the technique with the lose_prompt functions. I used a,b,c because I got lazy lol, but yeah I'll try to be more specific next time. Also that was a big brain fart with the c!='y' check, haha. Thanks again. \$\endgroup\$ – Jill and Jill May 14 '19 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.