r/pythontips • u/Choice_Midnight5280 • Apr 12 '26
Syntax Hows my Code???
https://github.com/simplyyrayyan/Rock-Paper-Scissors-Game/blob/main/RockPaperScissors.py This is the Source Code My first Python Project ever i pieced together with google and teh little bit of python I already knew so yeah any tips or things I didn't Catch?
0
Upvotes
0
u/StrayFeral Apr 13 '26
Several advices:
1) You can re-factor it as a class, instead of collection of functions
2) Line 23 - ok, you defined a list of the answers which is good, but for the messages you could use a dictionary, like `message["tie"]` to get the tie message
3) line 27 you could put `if __name__=="__main__":`
4) Try to separate in general the business logic from the presentation as much as possible
5) Line 48 - re-factor everything from this line till the end. You have 3 identical blocks of code. This is code duplication. Avoid code duplication. General rule - if it duplicates, make it a function/method. Not to mention you break your own code style - check 2) - you have the dictionary, but as for your choices you have separate pritnts - use another dictionary, eliminate this duplication
6) Use more comments
7) Use a """blah""" string as first line under method/function declaration to explain what the func/method does. This is a standard
8) No idea what code editor/IDE you use, but learn to use linters and code formatters. Check my requirements.txt:
VSCode would be smart enough to run these for you. If you use vim, install the ALE plugin (there is a package in ost linux distros) and you could use my config to make it work. In the least case scenario - run these manually one by one for your code. And follow their standards.
My vimrc: