Skip to main content
edited tags
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
class Player:

    def __init__(self):
        """Initializes the player object's attributes."""
        self.username = "Player_1"
        self.guess = None
        self.wins = 0

    def create_username(self):
        """Prompts the player to enter their username."""
        self.username = input("What is your name?: ")

    def get_username(self):
        """Returns the player's username as a string."""
        return self.username
    
    def get_guess(self):
        """Returns the player's guess as an integer."""

        is_valid_input = False

        while not is_valid_input:
            
            try:
                self.guess = int(input("Can you guess what it is?: "))
                is_valid_input = True
                return self.guess
            
            except ValueError:
                print()
                print("Please enter a whole number.")

    def add_win(self):
        """Adds a win to the player's win count."""
        self.wins += 1

    def get_wins(self):
        """Returns the player's win count as a string."""
        return str(self.wins)
class Player:

def __init__(self):
    """Initializes the player object's attributes."""
    self.username = "Player_1"
    self.guess = None
    self.wins = 0

def create_username(self):
    """Prompts the player to enter their username."""
    self.username = input("What is your name?: ")

def get_username(self):
    """Returns the player's username as a string."""
    return self.username

def get_guess(self):
    """Returns the player's guess as an integer."""

    is_valid_input = False

    while not is_valid_input:
        
        try:
            self.guess = int(input("Can you guess what it is?: "))
            is_valid_input = True
            return self.guess
        
        except ValueError:
            print()
            print("Please enter a whole number.")

def add_win(self):
    """Adds a win to the player's win count."""
    self.wins += 1

def get_wins(self):
    """Returns the player's win count as a string."""
    return str(self.wins)
class Player:

    def __init__(self):
        """Initializes the player object's attributes."""
        self.username = "Player_1"
        self.guess = None
        self.wins = 0

    def create_username(self):
        """Prompts the player to enter their username."""
        self.username = input("What is your name?: ")

    def get_username(self):
        """Returns the player's username as a string."""
        return self.username
    
    def get_guess(self):
        """Returns the player's guess as an integer."""

        is_valid_input = False

        while not is_valid_input:
            
            try:
                self.guess = int(input("Can you guess what it is?: "))
                is_valid_input = True
                return self.guess
            
            except ValueError:
                print()
                print("Please enter a whole number.")

    def add_win(self):
        """Adds a win to the player's win count."""
        self.wins += 1

    def get_wins(self):
        """Returns the player's win count as a string."""
        return str(self.wins)
Source Link

RNG Minigame In Python - Best Practices & Possible Improvements?

Recently, I got into programming with Python as a fun little side hobby. After watching a bunch of YouTube tutorials (to learn about IDE's, variables, etc.), I finally got to the point where I was able to make this really simple game where the player tries to guess the number that the computer generated. So, today I was hoping to get some feedback on the code I wrote.

I'm mostly interested in learning about the best practices for this language, but any criticism is welcome:

player.py

class Player:

def __init__(self):
    """Initializes the player object's attributes."""
    self.username = "Player_1"
    self.guess = None
    self.wins = 0

def create_username(self):
    """Prompts the player to enter their username."""
    self.username = input("What is your name?: ")

def get_username(self):
    """Returns the player's username as a string."""
    return self.username

def get_guess(self):
    """Returns the player's guess as an integer."""

    is_valid_input = False

    while not is_valid_input:
        
        try:
            self.guess = int(input("Can you guess what it is?: "))
            is_valid_input = True
            return self.guess
        
        except ValueError:
            print()
            print("Please enter a whole number.")

def add_win(self):
    """Adds a win to the player's win count."""
    self.wins += 1

def get_wins(self):
    """Returns the player's win count as a string."""
    return str(self.wins)

random_number_guesser.py

from player import Player
import random

class Random_Number_Guesser:

    def __init__(self):
        """Initializes the game object's attributes."""
        self.player = None
        self.winning_number = None

    def create_player(self):
        """Creates a new player object."""
        self.player = Player()
        self.player.create_username()

    def greet_player(self):
        """Greets the player."""
        print("Welcome, " + self.player.get_username() + "!")

    def generate_random_number(self):
        """Generates a winning number."""
        self.winning_number = random.randint(1, 10)
        print()
        print("The winning number is: " + str(self.winning_number) + ".")
        print("I'm thinking of a number between 1 & 10.")
        
    def guess_and_check(self):
        """Compares the player's guess to the winning number."""
        
        # Checks if the player's guess is correct.
        if self.player.get_guess() == self.winning_number:
            print()
            print(self.player.get_username() + " Won!")
            self.player.add_win()
        else:
            print()
            print(self.player.get_username() + " Lost!")

    def play_again(self):
        """Determines whether or not the player wants to play again."""

        is_finished_playing = False

        while not is_finished_playing:

            play_again = input("Would you like to play again? (y/n): ")

            # Checks if the player wants to play again & handles invalid inputs.
            if play_again == "y":
                self.generate_random_number()
                self.guess_and_check()

            elif play_again == "n":
                print()
                # Checks the player's win count and determines if 'time' or 'times' should be used.
                if int(self.player.get_wins()) > 1 or int(self.player.get_wins()) == 0:
                    print(self.player.get_username() + " won " + self.player.get_wins() + " times!")
                else:
                    print(self.player.get_username() + " won " + self.player.get_wins() + " time!")
                break

            else:
                print()
                print("Please enter either 'y' or 'n'.")

def play_random_number_guesser():
    print("Welcome To Random Number Guesser!")
    print("=================================")

    game = Random_Number_Guesser()
    game.create_player()
    game.greet_player()
    game.generate_random_number()
    game.guess_and_check()
    game.play_again()

game.py

from random_number_guesser import *

if __name__ == "__main__":
    play_random_number_guesser()