The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

1

Here I will modify the strings you printed such that it doesn't go over the screen. I don't see why you need to have GAME RULES as a separate variable. No harm and maybe more straightforward doing print("-----------------------------------") print("| |") print("| ROCK PAPER SCISSORS |") print("| PYTHON ...


3

Honestly, for the symbol, I'd just go with using uppercase characters (X and O) since the pieces don't really have any special functionality at this stage, and it's good to keep some simplicity with earlier projects. I'd put the win condition checking into the game class. If you want to reuse the board class in the future, it's a good idea for you to avoid ...


3

Your code is somewhat messy because almost everything is inside a single Program class. It can use some abstractions. Few things off the bat: 1) Declare and implement a strongly typed api for your service. For example: interface IChuckService { Joke[] GetRandomJokes(Category category); Category[] GetCategories(); Name[] GetNames(); //other ...


1

This code looks like a good start. Some variable could be declared with const because they aren't re-assigned - e.g. canvas, snake if the first line of newSnake() was changed to snake.length = 0;. This helps avoid accidental re-assignment in the future. The function name pGame could be improved for readability to be more descriptive of what it does - ...


3

Nice job. Here's two comments to add to what other's have said. range() excludes the stop point The built-in function range(start, stop, step) does not include stop in the items it returns. So in generate_card(): min_ = 1 max_ = 15 ... random.sample(range(min_, max_), 5) only selects from the numbers 1, 2, 3, ..., 14. max_ should be 16. I sometimes ...


4

Initializing your bingo card You don't need to initialize your dictionary with lists, since random.sample returns a list by default. Just iterate through the string "BINGO" to set your keys. Also, the if check doesn't need to happen, you know that you will be replacing the 3rd element of the list at the key 'N' every time, so just have that be an ...


6

Style Python comes with an "official" Style Guide for Python Code (aka PEP 8) which is a widely accepted standard for naming conventions and the like. Since you say your professor is keen on using best practices, I'd recommend to have a look at those guidelines. There is also a variety of tools that can help you to check for those best practices ...


3

Some low-hanging fruit: if win == True can be written as if win: Know that print delimits by a newline by default; you don't need to explicitly add \n unless you want two newlines. You can use classes to make card a class on its own.


8

Style I suggest you check PEP0008 https://www.python.org/dev/peps/pep-0008/ the official Python style guide which will be very useful when it comes to writing a more Pythonic code. Docstrings: Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. An object's ...


4

You have a lot of places where you do something like if int(p1) > int(p2): who_won.append(1) else: who_won.append(2) There's more duplication than necessary here. At the very least, you should move the call to append out so it's only called once. If you every change how who_won works, you don't want to have to change a ton of things all over. ...


1

I haven't looked at any Prolog code in at least a decade. So, I really can't give any useful feedback on your code. However, the question piqued my interest, and you did say you'd be more happy with ... Python...... So I rolled my own solver in Python. It's rough and under-tested, has very little error checking, and could use some refactoring. But ...


3

Bearing in mind that this code was posted ~2.5 years ago, I have some feedback. Perhaps you have learned a lot since then and the information below might not be new but it might help others as well. General Feedabck Overall the code looks okay. Readability is pretty good, though the path through the helper functions is a little tricky to comprehend at ...


2

files Python is not Java. Multiple classes can be in the same module (file) grid There is a lot of repetition between Ocean and Radar. You can use a lot of overlapping code enum instead of "." or "~" to denote what is in the grid, you could use Enums, and let the board representation method (view_radar for example) take care of how to represent this. ...


4

Use your own Interfaces You've created both your Ocean class and Radar class to accept a point key in the __getitem__ and __setitem__ methods. Ie) def __getitem__(self, point): row, col = point return self.ocean[row][col] def __setitem__(self, point, value): row, col = point self.ocean[row][col] = value But in your Player class, you ...


16

While it's clear that you're new to python, it's still pretty good that you got it to run first time. Good job! Input Validation Currently, you have a list of forbidden characters. While that can work, by default python allows unicode input. That means there's literally thousands of letters someone can input. I suggest instead using a whitelist of valid ...


1

add_member_to acts on family_tree and changes its fields. It is more logical to define the method in the class Family as add_member instead. The Family class should directly store a list of Persons as a field rather than copying all the information of the family members. Information of members can be directly retrieved from Person objects when needed. This ...


1

My one improvement would be this line of code if self.death is None: family_tree.deaths.append('N/A') else: family_tree.deaths.append(self.death) can be reduced to one line: family_tree.deaths.append('N/A' if self.death is None else self.death) Also, I would recommend keeping all code not in these classes in a main guard, so you can import these ...


3

Type hints PEP484 type hints will help document and test your code. For example, def can_use_col(self, row, col, size): can be (I guess) def can_use_col(self, row: int, col: int, size: int) -> bool: In-place addition col = col + 1 can be col += 1 Logic-by-exception This: try: self.ocean[row] return True except IndexError:...


2

I think everyone has some code they are embarrassed and not proud of and today I have decided to show mine. A pattern to get out of this mess .. That's an impressive amount of nested if-statements. As you can see, the behavior of collision handling depends both on the current instance's type and the provided instance's type. There is a pattern suited for ...


3

You use transform instead of fully qualifying you mean the one from std std::transform leading me to believe there's a user-defined transform function. Try to be specific with the functions you call.


7

Welcome to Code Review! On my first scan through the code I was impressed. I learned a new C++ STL library function, std::transform(). I was really glad to see that there was no using namespace std; statement in the code. Keep up the good work! Upon executing the program I found one problem: It might be better to change the prompt at the end of the game to ...


8

1. Use classes to separate concerns One improvement could be to introduce your own classes to separate the hangman drawing, from the main game loop. Something like class Gallow { private: static std::string gallows[] = { " | O\n | /|\\\n | |\n | / \\", // right leg (complete) " | O\n | /|\\\n | |\n | /", // left leg ...


11

You're doing some weird things with the scripts. I consider the first to be better, since especially in the second one, you're doing a lot of things with variables which we have better ways to do. I'll point those out separately - lets first have a look at your first script. Your first script Code Duplication If you find yourself typing the same thing a ...


1

I am glad to see that you separated the components of the program into different source files. Here's my suggestions: Game Object Avoid using namespace sf;. Especially in a header. The problem of using-directives are especially apparent with third-party libraries — the source of the names is obfuscated and there's the risk of name clashing. For ...


Top 50 recent answers are included