Skip to main content
added 293 characters in body
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

You have duplicate code in a couple places, specifically where you ask for input, then check if the number is correct or not. Consider breaking your code into some functions for that. For example:

def print_result(guess, target):
    """
    Print if guess is too high or low.
    Assumes they were already checked for equality.
    """
    if guess > target:
        print(" Too high, guess again")
    else:
        print(" Too low, guess again")

You have duplicate code in a couple places, specifically where you ask for input, then check if the number is correct or not. Consider breaking your code into some functions for that.

You have duplicate code in a couple places, specifically where you ask for input, then check if the number is correct or not. Consider breaking your code into some functions for that. For example:

def print_result(guess, target):
    """
    Print if guess is too high or low.
    Assumes they were already checked for equality.
    """
    if guess > target:
        print(" Too high, guess again")
    else:
        print(" Too low, guess again")
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

This is a good first question and a good first coding attempt!

Layout

Conventionally the imports are placed at the top of the file, before any other statements:

import random

print(" Welcome to the number guessing game")
play_game = True

The code is very consistent in its use of whitespace (indentation, space around operators, etc.). However, there are a couple places which could be slightly improved. The black program can be used to automatically add consistency.

UX

When I run the code, I get this prompt:

Your number are between

That wording is a little awkward. Consider this instead:

print(f"Guess a number between {random_integer1} and {random_integer2}")

Note that it uses an f-string to simplify the code.

For input prompts, I like to add a single space at the end of the text so that there will be a space between the text and my input on my screen:

number = int(input("what is your guess? "))

It is great that you specifically give the user the expected options for input ("y/n"). It is also common to allow input to be case-insensitive by calling lower():

play_again = input(" Do you want to play again? (y/n) ").lower()

Ouch!

Goodbye Coward

Be nice to your users so they'll want to come back and play again :)

Simpler

Here is another print line:

print("Correct, only took you", guess_count+1, "tries!")

which is commonly written using an f-string

print(f"Correct, only took you {guess_count+1} tries!")

In this code:

if play_again == 'y':
    play_game = False
    play_game = True

the following line is not needed and should be deleted:

    play_game = False

These 2 if statements:

if number > random_integer:
    print(" Too high, guess again")
if number < random_integer:
    print(" Too low, guess again")

can be simplified as an if/else:

if number > random_integer:
    print(" Too high, guess again")
else:
    print(" Too low, guess again")

There is no need for the extra check since you already checked for equality earlier on.

The same is true here:

if number == random_integer:
    running = False
    print("Correct first try")
if number != random_integer:

This is simpler:

if number == random_integer:
    running = False
    print("Correct first try")
else:

DRY

You have duplicate code in a couple places, specifically where you ask for input, then check if the number is correct or not. Consider breaking your code into some functions for that.

Documentation

The PEP 8 style guide recommends adding a docstring at the top of the code.

"""
Number-guessing game

Here's how you play... add description here
"""